9 Elf32_Word debug_section;
11 Elf32_Off orig_offset;
12 } UnstripInfoSection32;
15 Elf64_Word debug_section;
17 Elf64_Off orig_offset;
18 } UnstripInfoSection64;
21 Elf32_Off orig_e_shoff;
23 UnstripInfoSection32 sections[1];
27 Elf64_Off orig_e_shoff;
29 UnstripInfoSection64 sections[1];
33 elf_32_to_file (uint32_t x, int file_is_little_endian)
35 volatile uint32_t out;
36 unsigned char *outbytes;
38 outbytes = (unsigned char *)&out;
39 if (file_is_little_endian)
41 outbytes[0] = (x >> 0) & 0xff;
42 outbytes[1] = (x >> 8) & 0xff;
43 outbytes[2] = (x >> 16) & 0xff;
44 outbytes[3] = (x >> 24) & 0xff;
48 outbytes[0] = (x >> 24) & 0xff;
49 outbytes[1] = (x >> 16) & 0xff;
50 outbytes[2] = (x >> 8) & 0xff;
51 outbytes[3] = (x >> 0) & 0xff;
58 elf_64_to_file (uint64_t x, int file_is_little_endian)
60 volatile uint64_t out;
61 unsigned char *outbytes;
64 outbytes = (unsigned char *)&out;
65 if (file_is_little_endian)
67 for (i = 0; i < 8; i++)
68 outbytes[i] = (x >> (8*i)) & 0xff;
72 for (i = 0; i < 8; i++)
73 outbytes[7-i] = (x >> (8*i)) & 0xff;
80 word32_to_file (Elf32_Word x, Elf *elf)
82 Elf32_Ehdr *ehdr = elf32_getehdr (elf);
83 return elf_32_to_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
87 off32_to_file (Elf32_Off x, Elf *elf)
89 Elf32_Ehdr *ehdr = elf32_getehdr (elf);
90 return elf_32_to_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
94 word64_to_file (Elf64_Word x, Elf *elf)
96 Elf64_Ehdr *ehdr = elf64_getehdr (elf);
97 return elf_32_to_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
101 off64_to_file (Elf64_Off x, Elf *elf)
103 Elf64_Ehdr *ehdr = elf64_getehdr (elf);
104 return elf_64_to_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
108 elf_32_from_file (uint32_t x, int file_is_little_endian)
110 unsigned char *inbytes;
112 inbytes = (unsigned char *)&x;
113 if (file_is_little_endian)
121 else /* big endian */
132 elf_64_from_file (uint64_t x, int file_is_little_endian)
134 unsigned char *inbytes;
136 inbytes = (unsigned char *)&x;
137 if (file_is_little_endian)
140 ((uint64_t)inbytes[0] << 0) |
141 ((uint64_t)inbytes[1] << 8) |
142 ((uint64_t)inbytes[2] << 16) |
143 ((uint64_t)inbytes[3] << 24) |
144 ((uint64_t)inbytes[4] << 32) |
145 ((uint64_t)inbytes[5] << 40) |
146 ((uint64_t)inbytes[6] << 48) |
147 ((uint64_t)inbytes[7] << 56);
149 else /* big endian */
152 ((uint64_t)inbytes[0] << 56) |
153 ((uint64_t)inbytes[1] << 48) |
154 ((uint64_t)inbytes[2] << 40) |
155 ((uint64_t)inbytes[3] << 32) |
156 ((uint64_t)inbytes[4] << 24) |
157 ((uint64_t)inbytes[5] << 16) |
158 ((uint64_t)inbytes[6] << 8) |
159 ((uint64_t)inbytes[7] << 0);
164 word32_from_file (Elf32_Word x, Elf *elf)
166 Elf32_Ehdr *ehdr = elf32_getehdr (elf);
167 return elf_32_from_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
171 off32_from_file (Elf32_Off x, Elf *elf)
173 Elf32_Ehdr *ehdr = elf32_getehdr (elf);
174 return elf_32_from_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
178 word64_from_file (Elf64_Word x, Elf *elf)
180 Elf64_Ehdr *ehdr = elf64_getehdr (elf);
181 return elf_32_from_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
185 off64_from_file (Elf64_Off x, Elf *elf)
187 Elf64_Ehdr *ehdr = elf64_getehdr (elf);
188 return elf_64_from_file (x, ehdr->e_ident[EI_DATA] & ELFDATA2LSB);
192 unstrip_info_to_data32 (UnstripInfo *info,
196 UnstripInfo32 *info32;
202 /* orig_e_shoff */ sizeof (Elf32_Off) +
203 /* n_sections */ sizeof (Elf32_Off) +
204 /* sections */ info->n_sections * sizeof (UnstripInfoSection32);
206 data->d_buf = calloc (1, data->d_size);
208 info32 = (UnstripInfo32 *) data->d_buf;
210 info32->orig_e_shoff = off32_to_file (info->orig_e_shoff, elf);
211 info32->n_sections = off32_to_file (info->n_sections, elf);
213 for (i = 0; i < info->n_sections; i++)
215 info32->sections[i].debug_section = word32_to_file (info->sections[i].debug_section, elf);
216 info32->sections[i].name = word32_to_file (info->sections[i].name, elf);
217 info32->sections[i].orig_offset = off32_to_file (info->sections[i].orig_offset, elf);
222 unstrip_info_to_data64 (UnstripInfo *info,
226 UnstripInfo64 *info64;
232 /* orig_e_shoff */ sizeof (Elf64_Off) +
233 /* n_sections */ sizeof (Elf64_Off) +
234 /* sections */ info->n_sections * sizeof (UnstripInfoSection64);
236 data->d_buf = calloc (1, data->d_size);
238 info64 = (UnstripInfo64 *) data->d_buf;
240 info64->orig_e_shoff = off64_to_file (info->orig_e_shoff, elf);
241 info64->n_sections = off64_to_file (info->n_sections, elf);
243 for (i = 0; i < info->n_sections; i++)
245 info64->sections[i].debug_section = word64_to_file (info->sections[i].debug_section, elf);
246 info64->sections[i].name = word64_to_file (info->sections[i].name, elf);
247 info64->sections[i].orig_offset = off64_to_file (info->sections[i].orig_offset, elf);
252 unstrip_info_to_data (UnstripInfo *info,
258 data->d_type = ELF_T_BYTE;
261 gelf_getehdr (elf, &ehdr);
262 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
263 unstrip_info_to_data32 (info, elf, data);
264 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
265 unstrip_info_to_data64 (info, elf, data);
267 fprintf (stderr, "Warning. unsupported elf class\n");
271 unstrip_info_from_data32 (UnstripInfo *info,
275 UnstripInfo32 *info32;
278 info32 = (UnstripInfo32 *) data->d_buf;
280 info->orig_e_shoff = off32_from_file (info32->orig_e_shoff, elf);
281 info->n_sections = off32_from_file (info32->n_sections, elf);
283 info->sections = calloc (info->n_sections, sizeof (UnstripInfoSection));
284 for (i = 0; i < info->n_sections; i++)
286 info->sections[i].debug_section = word32_from_file (info32->sections[i].debug_section, elf);
287 info->sections[i].name = word32_from_file (info32->sections[i].name, elf);
288 info->sections[i].orig_offset = off32_from_file (info32->sections[i].orig_offset, elf);
293 unstrip_info_from_data64 (UnstripInfo *info,
297 UnstripInfo64 *info64;
300 info64 = (UnstripInfo64 *) data->d_buf;
302 info->orig_e_shoff = off64_from_file (info64->orig_e_shoff, elf);
303 info->n_sections = off64_from_file (info64->n_sections, elf);
305 info->sections = calloc (info->n_sections, sizeof (UnstripInfoSection));
306 for (i = 0; i < info->n_sections; i++)
308 info->sections[i].debug_section = word64_from_file (info64->sections[i].debug_section, elf);
309 info->sections[i].name = word64_from_file (info64->sections[i].name, elf);
310 info->sections[i].orig_offset = off64_from_file (info64->sections[i].orig_offset, elf);
315 unstrip_info_from_data (Elf *elf,
322 info = malloc (sizeof (UnstripInfo));
324 gelf_getehdr (elf, &ehdr);
325 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
326 unstrip_info_from_data32 (info, elf, data);
327 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
328 unstrip_info_from_data64 (info, elf, data);
330 fprintf (stderr, "Warning. unsupported elf class\n");
336 debug_link_to_data32 (DebugLink *debuglink,
340 size_t namelen_aligned;
345 namelen_aligned = align_up (strlen(debuglink->filename) + 1, 4);
348 /* name */ namelen_aligned +
349 /* checksum */ sizeof (Elf32_Word);
351 data->d_buf = calloc (1, data->d_size);
353 strcpy (data->d_buf, debuglink->filename);
354 p = ((char *)data->d_buf) + namelen_aligned;
356 *(Elf32_Word *)p = word32_to_file (debuglink->checksum, elf);
360 debug_link_to_data64 (DebugLink *debuglink,
364 size_t namelen_aligned;
369 namelen_aligned = align_up (strlen(debuglink->filename) + 1, 4);
372 /* name */ namelen_aligned +
373 /* checksum */ sizeof (Elf64_Word);
375 data->d_buf = calloc (1, data->d_size);
377 strcpy (data->d_buf, debuglink->filename);
378 p = ((char *)data->d_buf) + namelen_aligned;
380 *(Elf64_Word *)p = word64_to_file (debuglink->checksum, elf);
384 debug_link_to_data (DebugLink *debuglink, Elf *elf, Elf_Data *data)
388 data->d_type = ELF_T_BYTE;
391 gelf_getehdr (elf, &ehdr);
392 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
393 debug_link_to_data32 (debuglink, elf, data);
394 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
395 debug_link_to_data64 (debuglink, elf, data);
397 fprintf (stderr, "Warning. unsupported elf class\n");
401 debug_link_from_data32 (DebugLink *debuglink,
405 size_t namelen_aligned;
408 debuglink->filename = strdup (data->d_buf);
410 namelen_aligned = align_up (strlen (debuglink->filename) + 1, 4);
412 p = ((char *)data->d_buf) + namelen_aligned;
414 debuglink->checksum = word32_from_file (*(Elf32_Word *)p, elf);
418 debug_link_from_data64 (DebugLink *debuglink,
422 size_t namelen_aligned;
425 debuglink->filename = strdup (data->d_buf);
427 namelen_aligned = align_up (strlen (debuglink->filename) + 1, 4);
429 p = ((char *)data->d_buf) + namelen_aligned;
431 debuglink->checksum = word64_from_file (*(Elf64_Word *)p, elf);
436 debug_link_from_data (Elf *elf, Elf_Data *data)
439 DebugLink *debuglink;
441 debuglink = malloc (sizeof (DebugLink));
443 gelf_getehdr (elf, &ehdr);
444 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
445 debug_link_from_data32 (debuglink, elf, data);
446 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
447 debug_link_from_data64 (debuglink, elf, data);
449 fprintf (stderr, "Warning. unsupported elf class\n");