Skip to content

Commit 16fcc67

Browse files
SciresMyellows8
authored andcommitted
elf2nso/elf2nro: Parse Build ID into header.
1 parent 4225376 commit 16fcc67

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/elf2nro.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,25 @@ int main(int argc, char* argv[]) {
184184
file_off += nro_hdr.Segments[i].Size;
185185
file_off = (file_off + 0xFFF) & ~0xFFF;
186186
}
187+
188+
/* Iterate over sections to find build id. */
189+
size_t cur_sect_hdr_ofs = hdr->e_shoff;
190+
for (unsigned int i = 0; i < hdr->e_shnum; i++) {
191+
Elf64_Shdr *cur_shdr = (Elf64_Shdr *)(elf + cur_sect_hdr_ofs);
192+
if (cur_shdr->sh_type == SHT_NOTE) {
193+
Elf64_Nhdr *note_hdr = (Elf64_Nhdr *)(elf + cur_shdr->sh_offset);
194+
u8 *note_name = (u8 *)((uintptr_t)note_hdr + sizeof(Elf64_Nhdr));
195+
u8 *note_desc = note_name + note_hdr->n_namesz;
196+
if (note_hdr->n_type == NT_GNU_BUILD_ID && note_hdr->n_namesz == 4 && memcmp(note_name, "GNU\x00", 4) == 0) {
197+
size_t build_id_size = note_hdr->n_descsz;
198+
if (build_id_size > 0x20) {
199+
build_id_size = 0x20;
200+
}
201+
memcpy(nro_hdr.BuildId, note_desc, build_id_size);
202+
}
203+
}
204+
cur_sect_hdr_ofs += hdr->e_shentsize;
205+
}
187206

188207
FILE* out = fopen(argv[2], "wb");
189208

src/elf2nso.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,25 @@ int main(int argc, char* argv[]) {
157157
nso_hdr.CompSz[i] = comp_sz[i];
158158
file_off += comp_sz[i];
159159
}
160+
161+
/* Iterate over sections to find build id. */
162+
size_t cur_sect_hdr_ofs = hdr->e_shoff;
163+
for (unsigned int i = 0; i < hdr->e_shnum; i++) {
164+
Elf64_Shdr *cur_shdr = (Elf64_Shdr *)(elf + cur_sect_hdr_ofs);
165+
if (cur_shdr->sh_type == SHT_NOTE) {
166+
Elf64_Nhdr *note_hdr = (Elf64_Nhdr *)(elf + cur_shdr->sh_offset);
167+
u8 *note_name = (u8 *)((uintptr_t)note_hdr + sizeof(Elf64_Nhdr));
168+
u8 *note_desc = note_name + note_hdr->n_namesz;
169+
if (note_hdr->n_type == NT_GNU_BUILD_ID && note_hdr->n_namesz == 4 && memcmp(note_name, "GNU\x00", 4) == 0) {
170+
size_t build_id_size = note_hdr->n_descsz;
171+
if (build_id_size > 0x20) {
172+
build_id_size = 0x20;
173+
}
174+
memcpy(nso_hdr.BuildId, note_desc, build_id_size);
175+
}
176+
}
177+
cur_sect_hdr_ofs += hdr->e_shentsize;
178+
}
160179

161180
FILE* out = fopen(argv[2], "wb");
162181

src/elf_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,5 +995,6 @@ typedef struct {
995995
#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */
996996
#define R_X86_64_IRELATIVE 37
997997

998+
#define NT_GNU_BUILD_ID 3 /* Note type for .note.gnu.build-id */
998999

9991000
#endif /* !_SYS_ELF_COMMON_H_ */

0 commit comments

Comments
 (0)