Skip to content

Commit 2b7cbdd

Browse files
committed
chore: shorten change
Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent 35a279f commit 2b7cbdd

File tree

1 file changed

+46
-55
lines changed

1 file changed

+46
-55
lines changed

postject-api.h

+46-55
Original file line numberDiff line numberDiff line change
@@ -44,55 +44,11 @@ static inline bool postject_has_resource() {
4444
}
4545

4646
#if defined(__linux__)
47-
struct postject__dl_iterate_phdr_data {
48-
void* data;
49-
size_t size;
50-
const char* elf_section_name;
51-
size_t elf_section_name_size;
52-
};
53-
5447
static int postject__dl_iterate_phdr_callback(struct dl_phdr_info* info,
5548
size_t size,
56-
void* opaque) {
57-
struct postject__dl_iterate_phdr_data* data =
58-
(struct postject__dl_iterate_phdr_data*)opaque;
59-
60-
// iterate program headers
61-
for (unsigned i = 0; i < info->dlpi_phnum; i++) {
62-
// skip everything but notes
63-
if (info->dlpi_phdr[i].p_type != PT_NOTE)
64-
continue;
65-
66-
// note segment starts at base address + segment virtual address
67-
uintptr_t pos = (info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
68-
uintptr_t end = (pos + info->dlpi_phdr[i].p_memsz);
69-
70-
// iterate through segment until we reach the end
71-
while (pos < end) {
72-
if (pos + sizeof(ElfW(Nhdr)) > end) {
73-
break; // invalid
74-
}
75-
76-
ElfW(Nhdr)* note = (ElfW(Nhdr)*)(uintptr_t)pos;
77-
if (note->n_namesz != 0 && note->n_descsz != 0 &&
78-
strncmp((char*)(pos + sizeof(ElfW(Nhdr))), data->elf_section_name,
79-
data->elf_section_name_size) == 0) {
80-
data->size = note->n_descsz;
81-
// advance past note header and aligned name
82-
// to get to description data
83-
data->data = (void*)((uintptr_t)note + sizeof(ElfW(Nhdr)) +
84-
roundup(note->n_namesz, 4));
85-
// found the note, so terminate the search by returning 1
86-
return 1;
87-
}
88-
89-
pos += (sizeof(ElfW(Nhdr)) + roundup(note->n_namesz, 4) +
90-
roundup(note->n_descsz, 4));
91-
}
92-
}
93-
94-
// wasn't able to find the note in the main executable program headers, so
95-
// terminate the search
49+
void* data) {
50+
// Snag the dl_phdr_info struct for the main program, then stop iterating
51+
*((struct dl_phdr_info*)data) = *info;
9652
return 1;
9753
}
9854
#endif
@@ -167,14 +123,49 @@ static const void* postject_find_resource(
167123
name = options->elf_section_name;
168124
}
169125

170-
struct postject__dl_iterate_phdr_data data;
171-
data.data = NULL;
172-
data.size = 0;
173-
data.elf_section_name = name;
174-
data.elf_section_name_size = sizeof(name);
175-
dl_iterate_phdr(postject__dl_iterate_phdr_callback, &data);
176-
*size = data.size;
177-
return data.data;
126+
struct dl_phdr_info main_program_info;
127+
dl_iterate_phdr(postject__dl_iterate_phdr_callback, &main_program_info);
128+
129+
uintptr_t p = (uintptr_t)main_program_info.dlpi_phdr;
130+
size_t n = main_program_info.dlpi_phnum;
131+
uintptr_t base_addr = main_program_info.dlpi_addr;
132+
133+
// iterate program header
134+
for (; n > 0; n--, p += sizeof(ElfW(Phdr))) {
135+
ElfW(Phdr)* phdr = (ElfW(Phdr)*)p;
136+
137+
// skip everything but notes
138+
if (phdr->p_type != PT_NOTE) {
139+
continue;
140+
}
141+
142+
// note segment starts at base address + segment virtual address
143+
uintptr_t pos = (base_addr + phdr->p_vaddr);
144+
uintptr_t end = (pos + phdr->p_memsz);
145+
146+
// iterate through segment until we reach the end
147+
while (pos < end) {
148+
if (pos + sizeof(ElfW(Nhdr)) > end) {
149+
break; // invalid
150+
}
151+
152+
ElfW(Nhdr)* note = (ElfW(Nhdr)*)(uintptr_t)pos;
153+
if (note->n_namesz != 0 && note->n_descsz != 0 &&
154+
strncmp((char*)(pos + sizeof(ElfW(Nhdr))), (char*)name,
155+
sizeof(name)) == 0) {
156+
*size = note->n_descsz;
157+
// advance past note header and aligned name
158+
// to get to description data
159+
return (void*)((uintptr_t)note + sizeof(ElfW(Nhdr)) +
160+
roundup(note->n_namesz, 4));
161+
}
162+
163+
pos += (sizeof(ElfW(Nhdr)) + roundup(note->n_namesz, 4) +
164+
roundup(note->n_descsz, 4));
165+
}
166+
}
167+
return NULL;
168+
178169
#elif defined(_WIN32)
179170
void* ptr = NULL;
180171
char* resource_name = NULL;

0 commit comments

Comments
 (0)