1 /* Copyright (C) 2001-2003, 2005, 2007, 2009-2011, 2016, 2017 Red Hat, Inc.
2    Written by Alexander Larsson <alexl@redhat.com>, 2002
3    Based on code by Jakub Jelinek <jakub@redhat.com>, 2001.
4    String/Line table rewriting by Mark Wielaard <mjw@redhat.com>, 2017.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19 
20 #include "system.h"
21 
22 /* Needed for libelf */
23 #define _FILE_OFFSET_BITS 64
24 
25 #include <assert.h>
26 #include <byteswap.h>
27 #include <endian.h>
28 #include <errno.h>
29 #include <error.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <inttypes.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <popt.h>
40 
41 #include <gelf.h>
42 #include <dwarf.h>
43 
44 
45 /* Unfortunately strtab manipulation functions were only officially added
46    to elfutils libdw in 0.167.  Before that there were internal unsupported
47    ebl variants.  While libebl.h isn't supported we'll try to use it anyway
48    if the elfutils we build against is too old.  */
49 #include <elfutils/version.h>
50 #if _ELFUTILS_PREREQ (0, 167)
51 #include <elfutils/libdwelf.h>
52 typedef Dwelf_Strent		Strent;
53 typedef Dwelf_Strtab		Strtab;
54 #define strtab_init		dwelf_strtab_init
55 #define strtab_add(X,Y)		dwelf_strtab_add(X,Y)
56 #define strtab_add_len(X,Y,Z)	dwelf_strtab_add_len(X,Y,Z)
57 #define strtab_free		dwelf_strtab_free
58 #define strtab_finalize		dwelf_strtab_finalize
59 #define strent_offset		dwelf_strent_off
60 #else
61 #include <elfutils/libebl.h>
62 typedef struct Ebl_Strent	Strent;
63 typedef struct Ebl_Strtab	Strtab;
64 #define strtab_init		ebl_strtabinit
65 #define strtab_add(X,Y)		ebl_strtabadd(X,Y,0)
66 #define strtab_add_len(X,Y,Z)	ebl_strtabadd(X,Y,Z)
67 #define strtab_free		ebl_strtabfree
68 #define strtab_finalize		ebl_strtabfinalize
69 #define strent_offset		ebl_strtaboffset
70 #endif
71 
72 #include <search.h>
73 
74 #include <rpm/rpmio.h>
75 #include <rpm/rpmpgp.h>
76 #include "tools/hashtab.h"
77 
78 #define DW_TAG_partial_unit 0x3c
79 #define DW_FORM_sec_offset 0x17
80 #define DW_FORM_exprloc 0x18
81 #define DW_FORM_flag_present 0x19
82 #define DW_FORM_ref_sig8 0x20
83 
84 char *base_dir = NULL;
85 char *dest_dir = NULL;
86 char *list_file = NULL;
87 int list_file_fd = -1;
88 int do_build_id = 0;
89 int no_recompute_build_id = 0;
90 char *build_id_seed = NULL;
91 
92 int show_version = 0;
93 
94 /* We go over the debug sections in two phases. In phase zero we keep
95    track of any needed changes and collect strings, indexes and
96    sizes. In phase one we do the actual replacements updating the
97    strings, indexes and writing out new debug sections. The following
98    keep track of various changes that might be needed. */
99 
100 /* Whether we need to do any literal string (DW_FORM_string) replacements
101    in debug_info. */
102 static bool need_string_replacement = false;
103 /* Whether we need to do any updates of the string indexes (DW_FORM_strp)
104    in debug_info for string indexes. */
105 static bool need_strp_update = false;
106 /* If the debug_line changes size we will need to update the
107    DW_AT_stmt_list attributes indexes in the debug_info. */
108 static bool need_stmt_update = false;
109 
110 /* Storage for dynamically allocated strings to put into string
111    table. Keep together in memory blocks of 16K. */
112 #define STRMEMSIZE (16 * 1024)
113 struct strmemblock
114 {
115   struct strmemblock *next;
116   char memory[0];
117 };
118 
119 /* We keep track of each index in the original string table and the
120    associated entry in the new table so we don't insert identical
121    strings into the new string table. If constructed correctly the
122    original strtab shouldn't contain duplicate strings anyway. Any
123    actual identical strings could be deduplicated, but searching for
124    and comparing the indexes is much faster than comparing strings
125    (and we don't have to construct replacement strings). */
126 struct stridxentry
127 {
128   uint32_t idx; /* Original index in the string table. */
129   Strent *entry; /* Entry in the new table. */
130 };
131 
132 /* Storage for new string table entries. Keep together in memory to
133    quickly search through them with tsearch. */
134 #define STRIDXENTRIES ((16 * 1024) / sizeof (struct stridxentry))
135 struct strentblock
136 {
137   struct strentblock *next;
138   struct stridxentry entry[0];
139 };
140 
141 /* All data to keep track of the existing and new string table. */
142 struct strings
143 {
144   Strtab *str_tab;			/* The new string table. */
145   char *str_buf;			/* New Elf_Data d_buf. */
146   struct strmemblock *blocks;		/* The first strmemblock. */
147   struct strmemblock *last_block;	/* The currently used strmemblock. */
148   size_t stridx;			/* Next free byte in last block. */
149   struct strentblock *entries;		/* The first string index block. */
150   struct strentblock *last_entries;	/* The currently used strentblock. */
151   size_t entryidx;			/* Next free entry in the last block. */
152   void *strent_root;			/* strent binary search tree root. */
153 };
154 
155 struct line_table
156 {
157   size_t old_idx;     /* Original offset. */
158   size_t new_idx;     /* Offset in new debug_line section. */
159   ssize_t size_diff;  /* Difference in (header) size. */
160   bool replace_dirs;  /* Whether to replace any dir paths.  */
161   bool replace_files; /* Whether to replace any file paths. */
162 
163   /* Header fields. */
164   uint32_t unit_length;
165   uint16_t version;
166   uint32_t header_length;
167   uint8_t min_instr_len;
168   uint8_t max_op_per_instr; /* Only if version >= 4 */
169   uint8_t default_is_stmt;
170   int8_t line_base;
171   uint8_t line_range;
172   uint8_t opcode_base;
173 };
174 
175 struct debug_lines
176 {
177   struct line_table *table; /* Malloc/Realloced. */
178   size_t size;              /* Total number of line_tables.
179 			       Updated by get_line_table. */
180   size_t used;              /* Used number of line_tables.
181 			       Updated by get_line_table. */
182   size_t debug_lines_len;   /* Total size of new debug_line section.
183 			       updated by edit_dwarf2_line. */
184   char *line_buf;           /* New Elf_Data d_buf. */
185 };
186 
187 typedef struct
188 {
189   Elf *elf;
190   GElf_Ehdr ehdr;
191   Elf_Scn **scn;
192   const char *filename;
193   int lastscn;
194   size_t phnum;
195   struct strings strings;
196   struct debug_lines lines;
197   GElf_Shdr shdr[0];
198 } DSO;
199 
200 static void
setup_lines(struct debug_lines * lines)201 setup_lines (struct debug_lines *lines)
202 {
203   lines->table = NULL;
204   lines->size = 0;
205   lines->used = 0;
206   lines->debug_lines_len = 0;
207   lines->line_buf = NULL;
208 }
209 
210 static void
destroy_lines(struct debug_lines * lines)211 destroy_lines (struct debug_lines *lines)
212 {
213   free (lines->table);
214   free (lines->line_buf);
215 }
216 
217 typedef struct
218 {
219   unsigned char *ptr;
220   uint32_t addend;
221   int ndx;
222 } REL;
223 
224 typedef struct
225 {
226   Elf64_Addr r_offset;
227   int ndx;
228 } LINE_REL;
229 
230 #define read_uleb128(ptr) ({		\
231   unsigned int ret = 0;			\
232   unsigned int c;			\
233   int shift = 0;			\
234   do					\
235     {					\
236       c = *ptr++;			\
237       ret |= (c & 0x7f) << shift;	\
238       shift += 7;			\
239     } while (c & 0x80);			\
240 					\
241   if (shift >= 35)			\
242     ret = UINT_MAX;			\
243   ret;					\
244 })
245 
246 #define write_uleb128(ptr,val) ({	\
247   uint32_t valv = (val);		\
248   do					\
249     {					\
250       unsigned char c = valv & 0x7f;	\
251       valv >>= 7;			\
252       if (valv)				\
253 	c |= 0x80;			\
254       *ptr++ = c;			\
255     }					\
256   while (valv);				\
257 })
258 
259 static uint16_t (*do_read_16) (unsigned char *ptr);
260 static uint32_t (*do_read_32) (unsigned char *ptr);
261 static void (*do_write_16) (unsigned char *ptr, uint16_t val);
262 static void (*do_write_32) (unsigned char *ptr, uint32_t val);
263 
264 static int ptr_size;
265 static int cu_version;
266 
267 static inline uint16_t
buf_read_ule16(unsigned char * data)268 buf_read_ule16 (unsigned char *data)
269 {
270   return data[0] | (data[1] << 8);
271 }
272 
273 static inline uint16_t
buf_read_ube16(unsigned char * data)274 buf_read_ube16 (unsigned char *data)
275 {
276   return data[1] | (data[0] << 8);
277 }
278 
279 static inline uint32_t
buf_read_ule32(unsigned char * data)280 buf_read_ule32 (unsigned char *data)
281 {
282   return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
283 }
284 
285 static inline uint32_t
buf_read_ube32(unsigned char * data)286 buf_read_ube32 (unsigned char *data)
287 {
288   return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
289 }
290 
291 static const char *
strptr(DSO * dso,int sec,off_t offset)292 strptr (DSO *dso, int sec, off_t offset)
293 {
294   Elf_Scn *scn;
295   Elf_Data *data;
296 
297   scn = dso->scn[sec];
298   if (offset >= 0 && (GElf_Addr) offset < dso->shdr[sec].sh_size)
299     {
300       data = NULL;
301       while ((data = elf_getdata (scn, data)) != NULL)
302 	{
303 	  if (data->d_buf
304 	      && offset >= data->d_off
305 	      && offset < data->d_off + data->d_size)
306 	    return (const char *) data->d_buf + (offset - data->d_off);
307 	}
308     }
309 
310   return NULL;
311 }
312 
313 
314 #define read_8(ptr) *ptr++
315 
316 #define read_16(ptr) ({					\
317   uint16_t ret = do_read_16 (ptr);			\
318   ptr += 2;						\
319   ret;							\
320 })
321 
322 #define read_32(ptr) ({					\
323   uint32_t ret = do_read_32 (ptr);			\
324   ptr += 4;						\
325   ret;							\
326 })
327 
328 REL *relptr, *relend;
329 int reltype;
330 
331 #define do_read_32_relocated(ptr) ({			\
332   uint32_t dret = do_read_32 (ptr);			\
333   if (relptr)						\
334     {							\
335       while (relptr < relend && relptr->ptr < ptr)	\
336 	++relptr;					\
337       if (relptr < relend && relptr->ptr == ptr)	\
338 	{						\
339 	  if (reltype == SHT_REL)			\
340 	    dret += relptr->addend;			\
341 	  else						\
342 	    dret = relptr->addend;			\
343 	}						\
344     }							\
345   dret;							\
346 })
347 
348 #define read_32_relocated(ptr) ({			\
349   uint32_t ret = do_read_32_relocated (ptr);		\
350   ptr += 4;						\
351   ret;							\
352 })
353 
354 static void
dwarf2_write_le16(unsigned char * p,uint16_t v)355 dwarf2_write_le16 (unsigned char *p, uint16_t v)
356 {
357   p[0] = v;
358   p[1] = v >> 8;
359 }
360 
361 static void
dwarf2_write_le32(unsigned char * p,uint32_t v)362 dwarf2_write_le32 (unsigned char *p, uint32_t v)
363 {
364   p[0] = v;
365   p[1] = v >> 8;
366   p[2] = v >> 16;
367   p[3] = v >> 24;
368 }
369 
370 static void
dwarf2_write_be16(unsigned char * p,uint16_t v)371 dwarf2_write_be16 (unsigned char *p, uint16_t v)
372 {
373   p[1] = v;
374   p[0] = v >> 8;
375 }
376 
377 static void
dwarf2_write_be32(unsigned char * p,uint32_t v)378 dwarf2_write_be32 (unsigned char *p, uint32_t v)
379 {
380   p[3] = v;
381   p[2] = v >> 8;
382   p[1] = v >> 16;
383   p[0] = v >> 24;
384 }
385 
386 #define write_8(ptr,val) ({	\
387   *ptr++ = (val);		\
388 })
389 
390 #define write_16(ptr,val) ({	\
391   do_write_16 (ptr,val);	\
392   ptr += 2;			\
393 })
394 
395 #define write_32(ptr,val) ({	\
396   do_write_32 (ptr,val);	\
397   ptr += 4;			\
398 })
399 
400 /* relocated writes can only be called immediately after
401    do_read_32_relocated.  ptr must be equal to relptr->ptr (or
402    relend). Might just update the addend. So relocations need to be
403    updated at the end.  */
404 
405 static bool rel_updated;
406 
407 #define do_write_32_relocated(ptr,val) ({ \
408   if (relptr && relptr < relend && relptr->ptr == ptr)	\
409     {							\
410       if (reltype == SHT_REL)				\
411 	do_write_32 (ptr, val - relptr->addend);	\
412       else						\
413 	{						\
414 	  relptr->addend = val;				\
415 	  rel_updated = true;				\
416 	}						\
417     }							\
418   else							\
419     do_write_32 (ptr,val);				\
420 })
421 
422 #define write_32_relocated(ptr,val) ({ \
423   do_write_32_relocated (ptr,val);     \
424   ptr += 4;			       \
425 })
426 
427 typedef struct debug_section
428   {
429     const char *name;
430     unsigned char *data;
431     Elf_Data *elf_data;
432     size_t size;
433     int sec, relsec;
434     REL *relbuf;
435     REL *relend;
436     struct debug_section *next; /* Only happens for COMDAT .debug_macro.  */
437   } debug_section;
438 
439 static debug_section debug_sections[] =
440   {
441 #define DEBUG_INFO	0
442 #define DEBUG_ABBREV	1
443 #define DEBUG_LINE	2
444 #define DEBUG_ARANGES	3
445 #define DEBUG_PUBNAMES	4
446 #define DEBUG_PUBTYPES	5
447 #define DEBUG_MACINFO	6
448 #define DEBUG_LOC	7
449 #define DEBUG_STR	8
450 #define DEBUG_FRAME	9
451 #define DEBUG_RANGES	10
452 #define DEBUG_TYPES	11
453 #define DEBUG_MACRO	12
454 #define DEBUG_GDB_SCRIPT	13
455     { ".debug_info", NULL, NULL, 0, 0, 0 },
456     { ".debug_abbrev", NULL, NULL, 0, 0, 0 },
457     { ".debug_line", NULL, NULL, 0, 0, 0 },
458     { ".debug_aranges", NULL, NULL, 0, 0, 0 },
459     { ".debug_pubnames", NULL, NULL, 0, 0, 0 },
460     { ".debug_pubtypes", NULL, NULL, 0, 0, 0 },
461     { ".debug_macinfo", NULL, NULL, 0, 0, 0 },
462     { ".debug_loc", NULL, NULL, 0, 0, 0 },
463     { ".debug_str", NULL, NULL, 0, 0, 0 },
464     { ".debug_frame", NULL, NULL, 0, 0, 0 },
465     { ".debug_ranges", NULL, NULL, 0, 0, 0 },
466     { ".debug_types", NULL, NULL, 0, 0, 0 },
467     { ".debug_macro", NULL, NULL, 0, 0, 0 },
468     { ".debug_gdb_scripts", NULL, NULL, 0, 0, 0 },
469     { NULL, NULL, NULL, 0, 0, 0 }
470   };
471 
472 static int
rel_cmp(const void * a,const void * b)473 rel_cmp (const void *a, const void *b)
474 {
475   REL *rela = (REL *) a, *relb = (REL *) b;
476 
477   if (rela->ptr < relb->ptr)
478     return -1;
479 
480   if (rela->ptr > relb->ptr)
481     return 1;
482 
483   return 0;
484 }
485 
486 /* Returns a malloced REL array, or NULL when there are no relocations
487    for this section.  When there are relocations, will setup relend,
488    as the last REL, and reltype, as SHT_REL or SHT_RELA.  */
489 static void
setup_relbuf(DSO * dso,debug_section * sec,int * reltype)490 setup_relbuf (DSO *dso, debug_section *sec, int *reltype)
491 {
492   int ndx, maxndx;
493   GElf_Rel rel;
494   GElf_Rela rela;
495   GElf_Sym sym;
496   GElf_Addr base = dso->shdr[sec->sec].sh_addr;
497   Elf_Data *symdata = NULL;
498   int rtype;
499   REL *relbuf;
500   Elf_Scn *scn;
501   Elf_Data *data;
502   int i = sec->relsec;
503 
504   /* No relocations, or did we do this already? */
505   if (i == 0 || sec->relbuf != NULL)
506     {
507       relptr = sec->relbuf;
508       relend = sec->relend;
509       return;
510     }
511 
512   scn = dso->scn[i];
513   data = elf_getdata (scn, NULL);
514   assert (data != NULL && data->d_buf != NULL);
515   assert (elf_getdata (scn, data) == NULL);
516   assert (data->d_off == 0);
517   assert (data->d_size == dso->shdr[i].sh_size);
518   maxndx = dso->shdr[i].sh_size / dso->shdr[i].sh_entsize;
519   relbuf = malloc (maxndx * sizeof (REL));
520   *reltype = dso->shdr[i].sh_type;
521   if (relbuf == NULL)
522     error (1, errno, "%s: Could not allocate memory", dso->filename);
523 
524   symdata = elf_getdata (dso->scn[dso->shdr[i].sh_link], NULL);
525   assert (symdata != NULL && symdata->d_buf != NULL);
526   assert (elf_getdata (dso->scn[dso->shdr[i].sh_link], symdata) == NULL);
527   assert (symdata->d_off == 0);
528   assert (symdata->d_size == dso->shdr[dso->shdr[i].sh_link].sh_size);
529 
530   for (ndx = 0, relend = relbuf; ndx < maxndx; ++ndx)
531     {
532       if (dso->shdr[i].sh_type == SHT_REL)
533 	{
534 	  gelf_getrel (data, ndx, &rel);
535 	  rela.r_offset = rel.r_offset;
536 	  rela.r_info = rel.r_info;
537 	  rela.r_addend = 0;
538 	}
539       else
540 	gelf_getrela (data, ndx, &rela);
541       gelf_getsym (symdata, ELF64_R_SYM (rela.r_info), &sym);
542       /* Relocations against section symbols are uninteresting in REL.  */
543       if (dso->shdr[i].sh_type == SHT_REL && sym.st_value == 0)
544 	continue;
545       /* Only consider relocations against .debug_str, .debug_line
546 	 and .debug_abbrev.  */
547       if (sym.st_shndx != debug_sections[DEBUG_STR].sec
548 	  && sym.st_shndx != debug_sections[DEBUG_LINE].sec
549 	  && sym.st_shndx != debug_sections[DEBUG_ABBREV].sec)
550 	continue;
551       rela.r_addend += sym.st_value;
552       rtype = ELF64_R_TYPE (rela.r_info);
553       switch (dso->ehdr.e_machine)
554 	{
555 	case EM_SPARC:
556 	case EM_SPARC32PLUS:
557 	case EM_SPARCV9:
558 	  if (rtype != R_SPARC_32 && rtype != R_SPARC_UA32)
559 	    goto fail;
560 	  break;
561 	case EM_386:
562 	  if (rtype != R_386_32)
563 	    goto fail;
564 	  break;
565 	case EM_PPC:
566 	case EM_PPC64:
567 	  if (rtype != R_PPC_ADDR32 && rtype != R_PPC_UADDR32)
568 	    goto fail;
569 	  break;
570 	case EM_S390:
571 	  if (rtype != R_390_32)
572 	    goto fail;
573 	  break;
574 	case EM_IA_64:
575 	  if (rtype != R_IA64_SECREL32LSB)
576 	    goto fail;
577 	  break;
578 	case EM_X86_64:
579 	  if (rtype != R_X86_64_32)
580 	    goto fail;
581 	  break;
582 	case EM_ALPHA:
583 	  if (rtype != R_ALPHA_REFLONG)
584 	    goto fail;
585 	  break;
586 #if defined(EM_AARCH64) && defined(R_AARCH64_ABS32)
587 	case EM_AARCH64:
588 	  if (rtype != R_AARCH64_ABS32)
589 	    goto fail;
590 	  break;
591 #endif
592 	case EM_68K:
593 	  if (rtype != R_68K_32)
594 	    goto fail;
595 	  break;
596 #if defined(EM_RISCV) && defined(R_RISCV_32)
597 	case EM_RISCV:
598 	  if (rtype != R_RISCV_32)
599 	    goto fail;
600 	  break;
601 #endif
602 	default:
603 	fail:
604 	  error (1, 0, "%s: Unhandled relocation %d in %s section",
605 		 dso->filename, rtype, sec->name);
606 	}
607       relend->ptr = sec->data
608 	+ (rela.r_offset - base);
609       relend->addend = rela.r_addend;
610       relend->ndx = ndx;
611       ++(relend);
612     }
613   if (relbuf == relend)
614     {
615       free (relbuf);
616       relbuf = NULL;
617       relend = NULL;
618     }
619   else
620     qsort (relbuf, relend - relbuf, sizeof (REL), rel_cmp);
621 
622   sec->relbuf = relbuf;
623   sec->relend = relend;
624   relptr = relbuf;
625 }
626 
627 /* Updates SHT_RELA section associated with the given section based on
628    the relbuf data. The relbuf data is freed at the end.  */
629 static void
update_rela_data(DSO * dso,struct debug_section * sec)630 update_rela_data (DSO *dso, struct debug_section *sec)
631 {
632   Elf_Data *symdata;
633   int relsec_ndx = sec->relsec;
634   Elf_Data *data = elf_getdata (dso->scn[relsec_ndx], NULL);
635   symdata = elf_getdata (dso->scn[dso->shdr[relsec_ndx].sh_link],
636 			 NULL);
637 
638   relptr = sec->relbuf;
639   relend = sec->relend;
640   while (relptr < relend)
641     {
642       GElf_Sym sym;
643       GElf_Rela rela;
644       int ndx = relptr->ndx;
645 
646       if (gelf_getrela (data, ndx, &rela) == NULL)
647 	error (1, 0, "Couldn't get relocation: %s",
648 	       elf_errmsg (-1));
649 
650       if (gelf_getsym (symdata, GELF_R_SYM (rela.r_info),
651 		       &sym) == NULL)
652 	error (1, 0, "Couldn't get symbol: %s", elf_errmsg (-1));
653 
654       rela.r_addend = relptr->addend - sym.st_value;
655 
656       if (gelf_update_rela (data, ndx, &rela) == 0)
657 	error (1, 0, "Couldn't update relocations: %s",
658 	       elf_errmsg (-1));
659 
660       ++relptr;
661     }
662   elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
663 
664   free (sec->relbuf);
665 }
666 
667 struct abbrev_attr
668   {
669     unsigned int attr;
670     unsigned int form;
671   };
672 
673 struct abbrev_tag
674   {
675     unsigned int entry;
676     unsigned int tag;
677     int nattr;
678     struct abbrev_attr attr[0];
679   };
680 
681 static hashval_t
abbrev_hash(const void * p)682 abbrev_hash (const void *p)
683 {
684   struct abbrev_tag *t = (struct abbrev_tag *)p;
685 
686   return t->entry;
687 }
688 
689 static int
abbrev_eq(const void * p,const void * q)690 abbrev_eq (const void *p, const void *q)
691 {
692   struct abbrev_tag *t1 = (struct abbrev_tag *)p;
693   struct abbrev_tag *t2 = (struct abbrev_tag *)q;
694 
695   return t1->entry == t2->entry;
696 }
697 
698 static void
abbrev_del(void * p)699 abbrev_del (void *p)
700 {
701   free (p);
702 }
703 
704 static htab_t
read_abbrev(DSO * dso,unsigned char * ptr)705 read_abbrev (DSO *dso, unsigned char *ptr)
706 {
707   htab_t h = htab_try_create (50, abbrev_hash, abbrev_eq, abbrev_del);
708   unsigned int attr, form;
709   struct abbrev_tag *t;
710   int size;
711   void **slot;
712 
713   if (h == NULL)
714     {
715 no_memory:
716       error (0, ENOMEM, "%s: Could not read .debug_abbrev", dso->filename);
717       if (h)
718         htab_delete (h);
719       return NULL;
720     }
721 
722   while ((attr = read_uleb128 (ptr)) != 0)
723     {
724       size = 10;
725       t = malloc (sizeof (*t) + size * sizeof (struct abbrev_attr));
726       if (t == NULL)
727         goto no_memory;
728       t->entry = attr;
729       t->nattr = 0;
730       slot = htab_find_slot (h, t, INSERT);
731       if (slot == NULL)
732         {
733 	  free (t);
734 	  goto no_memory;
735         }
736       if (*slot != NULL)
737 	{
738 	  error (0, 0, "%s: Duplicate DWARF abbreviation %d", dso->filename,
739 		 t->entry);
740 	  free (t);
741 	  htab_delete (h);
742 	  return NULL;
743 	}
744       t->tag = read_uleb128 (ptr);
745       ++ptr; /* skip children flag.  */
746       while ((attr = read_uleb128 (ptr)) != 0)
747         {
748 	  if (t->nattr == size)
749 	    {
750 	      size += 10;
751 	      t = realloc (t, sizeof (*t) + size * sizeof (struct abbrev_attr));
752 	      if (t == NULL)
753 		goto no_memory;
754 	    }
755 	  form = read_uleb128 (ptr);
756 	  if (form == 2
757 	      || (form > DW_FORM_flag_present && form != DW_FORM_ref_sig8))
758 	    {
759 	      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename, form);
760 	      htab_delete (h);
761 	      return NULL;
762 	    }
763 
764 	  t->attr[t->nattr].attr = attr;
765 	  t->attr[t->nattr++].form = form;
766         }
767       if (read_uleb128 (ptr) != 0)
768         {
769 	  error (0, 0, "%s: DWARF abbreviation does not end with 2 zeros",
770 		 dso->filename);
771 	  htab_delete (h);
772 	  return NULL;
773         }
774       *slot = t;
775     }
776 
777   return h;
778 }
779 
780 #define IS_DIR_SEPARATOR(c) ((c)=='/')
781 
782 static char *
canonicalize_path(const char * s,char * d)783 canonicalize_path (const char *s, char *d)
784 {
785   char *rv = d;
786   char *droot;
787 
788   if (IS_DIR_SEPARATOR (*s))
789     {
790       *d++ = *s++;
791       if (IS_DIR_SEPARATOR (*s) && !IS_DIR_SEPARATOR (s[1]))
792 	{
793 	  /* Special case for "//foo" meaning a Posix namespace
794 	     escape.  */
795 	  *d++ = *s++;
796 	}
797       while (IS_DIR_SEPARATOR (*s))
798 	s++;
799     }
800   droot = d;
801 
802   while (*s)
803     {
804       /* At this point, we're always at the beginning of a path
805 	 segment.  */
806 
807       if (s[0] == '.' && (s[1] == 0 || IS_DIR_SEPARATOR (s[1])))
808 	{
809 	  s++;
810 	  if (*s)
811 	    while (IS_DIR_SEPARATOR (*s))
812 	      ++s;
813 	}
814 
815       else if (s[0] == '.' && s[1] == '.'
816 	       && (s[2] == 0 || IS_DIR_SEPARATOR (s[2])))
817 	{
818 	  char *pre = d - 1; /* includes slash */
819 	  while (droot < pre && IS_DIR_SEPARATOR (*pre))
820 	    pre--;
821 	  if (droot <= pre && ! IS_DIR_SEPARATOR (*pre))
822 	    {
823 	      while (droot < pre && ! IS_DIR_SEPARATOR (*pre))
824 		pre--;
825 	      /* pre now points to the slash */
826 	      if (droot < pre)
827 		pre++;
828 	      if (pre + 3 == d && pre[0] == '.' && pre[1] == '.')
829 		{
830 		  *d++ = *s++;
831 		  *d++ = *s++;
832 		}
833 	      else
834 		{
835 		  d = pre;
836 		  s += 2;
837 		  if (*s)
838 		    while (IS_DIR_SEPARATOR (*s))
839 		      s++;
840 		}
841 	    }
842 	  else
843 	    {
844 	      *d++ = *s++;
845 	      *d++ = *s++;
846 	    }
847 	}
848       else
849 	{
850 	  while (*s && ! IS_DIR_SEPARATOR (*s))
851 	    *d++ = *s++;
852 	}
853 
854       if (IS_DIR_SEPARATOR (*s))
855 	{
856 	  *d++ = *s++;
857 	  while (IS_DIR_SEPARATOR (*s))
858 	    s++;
859 	}
860     }
861   while (droot < d && IS_DIR_SEPARATOR (d[-1]))
862     --d;
863   if (d == rv)
864     *d++ = '.';
865   *d = 0;
866 
867   return rv;
868 }
869 
870 /* Returns the rest of PATH if it starts with DIR_PREFIX, skipping any
871    / path separators, or NULL if PATH doesn't start with
872    DIR_PREFIX. Might return the empty string if PATH equals DIR_PREFIX
873    (modulo trailing slashes). Never returns path starting with '/'.
874    Note that DIR_PREFIX itself should NOT end with a '/'.  */
875 static const char *
skip_dir_prefix(const char * path,const char * dir_prefix)876 skip_dir_prefix (const char *path, const char *dir_prefix)
877 {
878   size_t prefix_len = strlen (dir_prefix);
879   if (strncmp (path, dir_prefix, prefix_len) == 0)
880     {
881       path += prefix_len;
882       /* Unless path == dir_prefix there should be at least one '/'
883 	 in the path (which we will skip).  Otherwise the path has
884 	 a different (longer) directory prefix.  */
885       if (*path != '\0' && !IS_DIR_SEPARATOR (*path))
886 	return NULL;
887       while (IS_DIR_SEPARATOR (path[0]))
888 	path++;
889       return path;
890     }
891 
892   return NULL;
893 }
894 
895 /* Most strings will be in the existing debug string table. But to
896    replace the base/dest directory prefix we need some new storage.
897    Keep new strings somewhat close together for faster comparison and
898    copying.  SIZE should be at least one (and includes space for the
899    zero terminator). The returned pointer points to uninitialized
900    data.  */
901 static char *
new_string_storage(struct strings * strings,size_t size)902 new_string_storage (struct strings *strings, size_t size)
903 {
904   assert (size > 0);
905 
906   /* If the string is extra long just create a whole block for
907      it. Normally strings are much smaller than STRMEMSIZE. */
908   if (strings->last_block == NULL
909       || size > STRMEMSIZE
910       || strings->stridx > STRMEMSIZE
911       || (STRMEMSIZE - strings->stridx) < size)
912     {
913       struct strmemblock *newblock = malloc (sizeof (struct strmemblock)
914 					     + MAX (STRMEMSIZE, size));
915       if (newblock == NULL)
916 	return NULL;
917 
918       newblock->next = NULL;
919 
920       if (strings->blocks == NULL)
921 	strings->blocks = newblock;
922 
923       if (strings->last_block != NULL)
924 	strings->last_block->next = newblock;
925 
926       strings->last_block = newblock;
927       strings->stridx = 0;
928     }
929 
930   size_t stridx = strings->stridx;
931   strings->stridx += size + 1;
932   return &strings->last_block->memory[stridx];
933 }
934 
935 /* Comparison function used for tsearch. */
936 static int
strent_compare(const void * a,const void * b)937 strent_compare (const void *a, const void *b)
938 {
939   struct stridxentry *entry_a = (struct stridxentry *)a;
940   struct stridxentry *entry_b = (struct stridxentry *)b;
941   size_t idx_a = entry_a->idx;
942   size_t idx_b = entry_b->idx;
943 
944   if (idx_a < idx_b)
945     return -1;
946 
947   if (idx_a > idx_b)
948     return 1;
949 
950   return 0;
951 }
952 
953 /* Allocates and inserts a new entry for the old index if not yet
954    seen.  Returns a stridxentry if the given index has not yet been
955    seen and needs to be filled in with the associated string (either
956    the original string or the replacement string). Returns NULL if the
957    idx is already known. Use in phase 0 to add all strings seen. In
958    phase 1 use string_find_entry instead to get existing entries. */
959 static struct stridxentry *
string_find_new_entry(struct strings * strings,size_t old_idx)960 string_find_new_entry (struct strings *strings, size_t old_idx)
961 {
962   /* Use next entry in the pool for lookup so we can use it directly
963      if this is a new index. */
964   struct stridxentry *entry;
965 
966   /* Keep entries close together to make key comparison fast. */
967   if (strings->last_entries == NULL || strings->entryidx >= STRIDXENTRIES)
968     {
969       size_t entriessz = (sizeof (struct strentblock)
970 			  + (STRIDXENTRIES * sizeof (struct stridxentry)));
971       struct strentblock *newentries = malloc (entriessz);
972       if (newentries == NULL)
973 	error (1, errno, "Couldn't allocate new string entries block");
974       else
975 	{
976 	  if (strings->entries == NULL)
977 	    strings->entries = newentries;
978 
979 	  if (strings->last_entries != NULL)
980 	    strings->last_entries->next = newentries;
981 
982 	  strings->last_entries = newentries;
983 	  strings->last_entries->next = NULL;
984 	  strings->entryidx = 0;
985 	}
986     }
987 
988   entry = &strings->last_entries->entry[strings->entryidx];
989   entry->idx = old_idx;
990   struct stridxentry **tres = tsearch (entry, &strings->strent_root,
991 				       strent_compare);
992   if (tres == NULL)
993     error (1, ENOMEM, "Couldn't insert new strtab idx");
994   else if (*tres == entry)
995     {
996       /* idx not yet seen, must add actual str.  */
997       strings->entryidx++;
998       return entry;
999     }
1000 
1001   return NULL; /* We already know about this idx, entry already complete. */
1002 }
1003 
1004 static struct stridxentry *
string_find_entry(struct strings * strings,size_t old_idx)1005 string_find_entry (struct strings *strings, size_t old_idx)
1006 {
1007   struct stridxentry **ret;
1008   struct stridxentry key;
1009   key.idx = old_idx;
1010   ret = tfind (&key, &strings->strent_root, strent_compare);
1011   assert (ret != NULL); /* Can only happen for a bad/non-existing old_idx. */
1012   return *ret;
1013 }
1014 
1015 /* Adds a string_idx_entry given an index into the old/existing string
1016    table. Should be used in phase 0. Does nothing if the index was
1017    already registered. Otherwise it checks the string associated with
1018    the index. If the old string doesn't start with base_dir an entry
1019    will be recorded for the index with the same string. Otherwise a
1020    string will be recorded where the base_dir prefix will be replaced
1021    by dest_dir. Returns true if this is a not yet seen index and there
1022    a replacement file string has been recorded for it, otherwise
1023    returns false.  */
1024 static bool
record_file_string_entry_idx(struct strings * strings,size_t old_idx)1025 record_file_string_entry_idx (struct strings *strings, size_t old_idx)
1026 {
1027   bool ret = false;
1028   struct stridxentry *entry = string_find_new_entry (strings, old_idx);
1029   if (entry != NULL)
1030     {
1031       if (old_idx >= debug_sections[DEBUG_STR].size)
1032 	error (1, 0, "Bad string pointer index %zd", old_idx);
1033 
1034       Strent *strent;
1035       const char *old_str = (char *)debug_sections[DEBUG_STR].data + old_idx;
1036       const char *file = skip_dir_prefix (old_str, base_dir);
1037       if (file == NULL)
1038 	{
1039 	  /* Just record the existing string.  */
1040 	  strent = strtab_add_len (strings->str_tab, old_str,
1041 				   strlen (old_str) + 1);
1042 	}
1043       else
1044 	{
1045 	  /* Create and record the altered file path. */
1046 	  size_t dest_len = strlen (dest_dir);
1047 	  size_t file_len = strlen (file);
1048 	  size_t nsize = dest_len + 1; /* + '\0' */
1049 	  if (file_len > 0)
1050 	    nsize += 1 + file_len;     /* + '/' */
1051 	  char *nname = new_string_storage (strings, nsize);
1052 	  if (nname == NULL)
1053 	    error (1, ENOMEM, "Couldn't allocate new string storage");
1054 	  memcpy (nname, dest_dir, dest_len);
1055 	  if (file_len > 0)
1056 	    {
1057 	      nname[dest_len] = '/';
1058 	      memcpy (nname + dest_len + 1, file, file_len + 1);
1059 	    }
1060 	  else
1061 	    nname[dest_len] = '\0';
1062 
1063 	  strent = strtab_add_len (strings->str_tab, nname, nsize);
1064 	  ret = true;
1065 	}
1066       if (strent == NULL)
1067 	error (1, ENOMEM, "Could not create new string table entry");
1068       else
1069 	entry->entry = strent;
1070     }
1071 
1072   return ret;
1073 }
1074 
1075 /* Same as record_new_string_file_string_entry_idx but doesn't replace
1076    base_dir with dest_dir, just records the existing string associated
1077    with the index. */
1078 static void
record_existing_string_entry_idx(struct strings * strings,size_t old_idx)1079 record_existing_string_entry_idx (struct strings *strings, size_t old_idx)
1080 {
1081   struct stridxentry *entry = string_find_new_entry (strings, old_idx);
1082   if (entry != NULL)
1083     {
1084       if (old_idx >= debug_sections[DEBUG_STR].size)
1085 	error (1, 0, "Bad string pointer index %zd", old_idx);
1086 
1087       const char *str = (char *)debug_sections[DEBUG_STR].data + old_idx;
1088       Strent *strent = strtab_add_len (strings->str_tab,
1089 				       str, strlen (str) + 1);
1090       if (strent == NULL)
1091 	error (1, ENOMEM, "Could not create new string table entry");
1092       else
1093 	entry->entry = strent;
1094     }
1095 }
1096 
1097 static void
setup_strings(struct strings * strings)1098 setup_strings (struct strings *strings)
1099 {
1100   strings->str_tab = strtab_init (false);
1101   strings->str_buf = NULL;
1102   strings->blocks = NULL;
1103   strings->last_block = NULL;
1104   strings->entries = NULL;
1105   strings->last_entries = NULL;
1106   strings->strent_root = NULL;
1107 }
1108 
1109 /* Noop for tdestroy. */
free_node(void * p)1110 static void free_node (void *p __attribute__((__unused__))) { }
1111 
1112 static void
destroy_strings(struct strings * strings)1113 destroy_strings (struct strings *strings)
1114 {
1115   struct strmemblock *smb = strings->blocks;
1116   while (smb != NULL)
1117     {
1118       void *old = smb;
1119       smb = smb->next;
1120       free (old);
1121     }
1122 
1123   struct strentblock *emb = strings->entries;
1124   while (emb != NULL)
1125     {
1126       void *old = emb;
1127       emb = emb->next;
1128       free (old);
1129     }
1130 
1131   strtab_free (strings->str_tab);
1132   tdestroy (strings->strent_root, &free_node);
1133   free (strings->str_buf);
1134 }
1135 
1136 /* The minimum number of line tables we pre-allocate. */
1137 #define MIN_LINE_TABLES 64
1138 
1139 /* Gets a line_table at offset. Returns true if not yet know and
1140    successfully read, false otherwise.  Sets *table to NULL and
1141    outputs a warning if there was a problem reading the table at the
1142    given offset.  */
1143 static bool
get_line_table(DSO * dso,size_t off,struct line_table ** table)1144 get_line_table (DSO *dso, size_t off, struct line_table **table)
1145 {
1146   struct debug_lines *lines = &dso->lines;
1147   /* Assume there aren't that many, just do a linear search.  The
1148      array is probably already sorted because the stmt_lists are
1149      probably inserted in order. But we cannot rely on that (maybe we
1150      should check that to make searching quicker if possible?).  Once
1151      we have all line tables for phase 1 (rewriting) we do explicitly
1152      sort the array.*/
1153   for (int i = 0; i < lines->used; i++)
1154     if (lines->table[i].old_idx == off)
1155       {
1156 	*table = &lines->table[i];
1157 	return false;
1158       }
1159 
1160   if (lines->size == lines->used)
1161     {
1162       struct line_table *new_table = realloc (lines->table,
1163 					      (sizeof (struct line_table)
1164 					       * (lines->size
1165 						  + MIN_LINE_TABLES)));
1166       if (new_table == NULL)
1167 	{
1168 	  error (0, ENOMEM, "Couldn't add more debug_line tables");
1169 	  *table = NULL;
1170 	  return false;
1171 	}
1172       lines->table = new_table;
1173       lines->size += MIN_LINE_TABLES;
1174     }
1175 
1176   struct line_table *t = &lines->table[lines->used];
1177   *table = NULL;
1178 
1179   t->old_idx = off;
1180   t->new_idx = off;
1181   t->size_diff = 0;
1182   t->replace_dirs = false;
1183   t->replace_files = false;
1184 
1185   unsigned char *ptr = debug_sections[DEBUG_LINE].data;
1186   unsigned char *endsec = ptr + debug_sections[DEBUG_LINE].size;
1187   if (ptr == NULL)
1188     {
1189       error (0, 0, "%s: No .line_table section", dso->filename);
1190       return false;
1191     }
1192 
1193   if (off > debug_sections[DEBUG_LINE].size)
1194     {
1195       error (0, 0, "%s: Invalid .line_table offset 0x%zx",
1196 	     dso->filename, off);
1197       return false;
1198     }
1199   ptr += off;
1200 
1201   /* unit_length */
1202   unsigned char *endcu = ptr + 4;
1203   t->unit_length = read_32 (ptr);
1204   endcu += t->unit_length;
1205   if (endcu == ptr + 0xffffffff)
1206     {
1207       error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
1208       return false;
1209     }
1210 
1211   if (endcu > endsec)
1212     {
1213       error (0, 0, "%s: .debug_line CU does not fit into section",
1214 	     dso->filename);
1215       return false;
1216     }
1217 
1218   /* version */
1219   t->version = read_16 (ptr);
1220   if (t->version != 2 && t->version != 3 && t->version != 4)
1221     {
1222       error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
1223 	     t->version);
1224       return false;
1225     }
1226 
1227   /* header_length */
1228   unsigned char *endprol = ptr + 4;
1229   t->header_length = read_32 (ptr);
1230   endprol += t->header_length;
1231   if (endprol > endcu)
1232     {
1233       error (0, 0, "%s: .debug_line CU prologue does not fit into CU",
1234 	     dso->filename);
1235       return false;
1236     }
1237 
1238   /* min instr len */
1239   t->min_instr_len = *ptr++;
1240 
1241   /* max op per instr, if version >= 4 */
1242   if (t->version >= 4)
1243     t->max_op_per_instr = *ptr++;
1244 
1245   /* default is stmt */
1246   t->default_is_stmt = *ptr++;
1247 
1248   /* line base */
1249   t->line_base = (*(int8_t *)ptr++);
1250 
1251   /* line range */
1252   t->line_range = *ptr++;
1253 
1254   /* opcode base */
1255   t->opcode_base = *ptr++;
1256 
1257   if (ptr + t->opcode_base - 1 >= endcu)
1258     {
1259       error (0, 0, "%s: .debug_line opcode table does not fit into CU",
1260 	     dso->filename);
1261       return false;
1262     }
1263   lines->used++;
1264   *table = t;
1265   return true;
1266 }
1267 
1268 static int dirty_elf;
1269 static void
dirty_section(unsigned int sec)1270 dirty_section (unsigned int sec)
1271 {
1272   elf_flagdata (debug_sections[sec].elf_data, ELF_C_SET, ELF_F_DIRTY);
1273   dirty_elf = 1;
1274 }
1275 
1276 static int
line_table_cmp(const void * a,const void * b)1277 line_table_cmp (const void *a, const void *b)
1278 {
1279   struct line_table *ta = (struct line_table *) a;
1280   struct line_table *tb = (struct line_table *) b;
1281 
1282   if (ta->old_idx < tb->old_idx)
1283     return -1;
1284 
1285   if (ta->old_idx > tb->old_idx)
1286     return 1;
1287 
1288   return 0;
1289 }
1290 
1291 
1292 /* Called after phase zero (which records all adjustments needed for
1293    the line tables referenced from debug_info) and before phase one
1294    starts (phase one will adjust the .debug_line section stmt
1295    references using the updated data structures). */
1296 static void
edit_dwarf2_line(DSO * dso)1297 edit_dwarf2_line (DSO *dso)
1298 {
1299   Elf_Data *linedata = debug_sections[DEBUG_LINE].elf_data;
1300   int linendx = debug_sections[DEBUG_LINE].sec;
1301   Elf_Scn *linescn = dso->scn[linendx];
1302   unsigned char *old_buf = linedata->d_buf;
1303 
1304   /* Out with the old. */
1305   linedata->d_size = 0;
1306 
1307   /* In with the new. */
1308   linedata = elf_newdata (linescn);
1309 
1310   dso->lines.line_buf = malloc (dso->lines.debug_lines_len);
1311   if (dso->lines.line_buf == NULL)
1312     error (1, ENOMEM, "No memory for new .debug_line table (0x%zx bytes)",
1313 	   dso->lines.debug_lines_len);
1314 
1315   linedata->d_size = dso->lines.debug_lines_len;
1316   linedata->d_buf = dso->lines.line_buf;
1317   debug_sections[DEBUG_LINE].size = linedata->d_size;
1318 
1319   /* Make sure the line tables are sorted on the old index. */
1320   qsort (dso->lines.table, dso->lines.used, sizeof (struct line_table),
1321 	 line_table_cmp);
1322 
1323   unsigned char *ptr = linedata->d_buf;
1324   for (int ldx = 0; ldx < dso->lines.used; ldx++)
1325     {
1326       struct line_table *t = &dso->lines.table[ldx];
1327       unsigned char *optr = old_buf + t->old_idx;
1328       t->new_idx = ptr - (unsigned char *) linedata->d_buf;
1329 
1330       /* Just copy the whole table if nothing needs replacing. */
1331       if (! t->replace_dirs && ! t->replace_files)
1332 	{
1333 	  assert (t->size_diff == 0);
1334 	  memcpy (ptr, optr, t->unit_length + 4);
1335 	  ptr += t->unit_length + 4;
1336 	  continue;
1337 	}
1338 
1339       /* Header fields. */
1340       write_32 (ptr, t->unit_length + t->size_diff);
1341       write_16 (ptr, t->version);
1342       write_32 (ptr, t->header_length + t->size_diff);
1343       write_8 (ptr, t->min_instr_len);
1344       if (t->version >= 4)
1345 	write_8 (ptr, t->max_op_per_instr);
1346       write_8 (ptr, t->default_is_stmt);
1347       write_8 (ptr, t->line_base);
1348       write_8 (ptr, t->line_range);
1349       write_8 (ptr, t->opcode_base);
1350 
1351       optr += (4 /* unit len */
1352 	       + 2 /* version */
1353 	       + 4 /* header len */
1354 	       + 1 /* min instr len */
1355 	       + (t->version >= 4) /* max op per instr, if version >= 4 */
1356 	       + 1 /* default is stmt */
1357 	       + 1 /* line base */
1358 	       + 1 /* line range */
1359 	       + 1); /* opcode base */
1360 
1361       /* opcode len table. */
1362       memcpy (ptr, optr, t->opcode_base - 1);
1363       optr += t->opcode_base - 1;
1364       ptr += t->opcode_base - 1;
1365 
1366       /* directory table. We need to find the end (start of file
1367 	 table) anyway, so loop over all dirs, even if replace_dirs is
1368 	 false. */
1369       while (*optr != 0)
1370 	{
1371 	  const char *dir = (const char *) optr;
1372 	  const char *file_path = NULL;
1373 	  if (t->replace_dirs)
1374 	    {
1375 	      file_path = skip_dir_prefix (dir, base_dir);
1376 	      if (file_path != NULL)
1377 		{
1378 		  size_t dest_len = strlen (dest_dir);
1379 		  size_t file_len = strlen (file_path);
1380 		  memcpy (ptr, dest_dir, dest_len);
1381 		  ptr += dest_len;
1382 		  if (file_len > 0)
1383 		    {
1384 		      *ptr++ = '/';
1385 		      memcpy (ptr, file_path, file_len);
1386 		      ptr += file_len;
1387 		    }
1388 		  *ptr++ = '\0';
1389 		}
1390 	    }
1391 	  if (file_path == NULL)
1392 	    {
1393 	      size_t dir_len = strlen (dir);
1394 	      memcpy (ptr, dir, dir_len + 1);
1395 	      ptr += dir_len + 1;
1396 	    }
1397 
1398 	  optr = (unsigned char *) strchr (dir, 0) + 1;
1399 	}
1400       optr++;
1401       *ptr++ = '\0';
1402 
1403       /* file table */
1404       if (t->replace_files)
1405 	{
1406 	  while (*optr != 0)
1407 	    {
1408 	      const char *file = (const char *) optr;
1409 	      const char *file_path = NULL;
1410 	      if (t->replace_files)
1411 		{
1412 		  file_path = skip_dir_prefix (file, base_dir);
1413 		  if (file_path != NULL)
1414 		    {
1415 		      size_t dest_len = strlen (dest_dir);
1416 		      size_t file_len = strlen (file_path);
1417 		      memcpy (ptr, dest_dir, dest_len);
1418 		      ptr += dest_len;
1419 		      if (file_len > 0)
1420 			{
1421 			  *ptr++ = '/';
1422 			  memcpy (ptr, file_path, file_len);
1423 			  ptr += file_len;
1424 			}
1425 		      *ptr++ = '\0';
1426 		    }
1427 		}
1428 	      if (file_path == NULL)
1429 		{
1430 		  size_t file_len = strlen (file);
1431 		  memcpy (ptr, file, file_len + 1);
1432 		  ptr += file_len + 1;
1433 		}
1434 
1435 	      optr = (unsigned char *) strchr (file, 0) + 1;
1436 
1437 	      /* dir idx, time, len */
1438 	      uint32_t dir_idx = read_uleb128 (optr);
1439 	      write_uleb128 (ptr, dir_idx);
1440 	      uint32_t time = read_uleb128 (optr);
1441 	      write_uleb128 (ptr, time);
1442 	      uint32_t len = read_uleb128 (optr);
1443 	      write_uleb128 (ptr, len);
1444 	    }
1445 	  optr++;
1446 	  *ptr++ = '\0';
1447 	}
1448 
1449       /* line number program (and file table if not copied above). */
1450       size_t remaining = (t->unit_length + 4
1451 			  - (optr - (old_buf + t->old_idx)));
1452       memcpy (ptr, optr, remaining);
1453       ptr += remaining;
1454     }
1455 }
1456 
1457 /* Called during phase zero for each debug_line table referenced from
1458    .debug_info.  Outputs all source files seen and records any
1459    adjustments needed in the debug_list data structures. Returns true
1460    if line_table needs to be rewrite either the dir or file paths. */
1461 static bool
read_dwarf2_line(DSO * dso,uint32_t off,char * comp_dir)1462 read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
1463 {
1464   unsigned char *ptr, *dir;
1465   unsigned char **dirt;
1466   uint32_t value, dirt_cnt;
1467   size_t comp_dir_len = !comp_dir ? 0 : strlen (comp_dir);
1468   struct line_table *table;
1469 
1470   if (get_line_table (dso, off, &table) == false
1471       || table == NULL)
1472     {
1473       if (table != NULL)
1474 	error (0, 0, ".debug_line offset 0x%x referenced multiple times",
1475 	       off);
1476       return false;
1477     }
1478 
1479   /* Skip to the directory table. The rest of the header has already
1480      been read and checked by get_line_table. */
1481   ptr = debug_sections[DEBUG_LINE].data + off;
1482   ptr += (4 /* unit len */
1483 	  + 2 /* version */
1484 	  + 4 /* header len */
1485 	  + 1 /* min instr len */
1486 	  + (table->version >= 4) /* max op per instr, if version >= 4 */
1487 	  + 1 /* default is stmt */
1488 	  + 1 /* line base */
1489 	  + 1 /* line range */
1490 	  + 1 /* opcode base */
1491 	  + table->opcode_base - 1); /* opcode len table */
1492   dir = ptr;
1493 
1494   /* dir table: */
1495   value = 1;
1496   while (*ptr != 0)
1497     {
1498       if (base_dir && dest_dir)
1499 	{
1500 	  /* Do we need to replace any of the dirs? Calculate new size. */
1501 	  const char *file_path = skip_dir_prefix ((const char *)ptr,
1502 						   base_dir);
1503 	  if (file_path != NULL)
1504 	    {
1505 	      size_t old_size = strlen ((const char *)ptr) + 1;
1506 	      size_t file_len = strlen (file_path);
1507 	      size_t new_size = strlen (dest_dir) + 1;
1508 	      if (file_len > 0)
1509 		new_size += 1 + file_len;
1510 	      table->size_diff += (new_size - old_size);
1511 	      table->replace_dirs = true;
1512 	    }
1513 	}
1514 
1515       ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1516       ++value;
1517     }
1518 
1519   dirt = (unsigned char **) alloca (value * sizeof (unsigned char *));
1520   dirt[0] = (unsigned char *) ".";
1521   dirt_cnt = 1;
1522   ptr = dir;
1523   while (*ptr != 0)
1524     {
1525       dirt[dirt_cnt++] = ptr;
1526       ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1527     }
1528   ptr++;
1529 
1530   /* file table: */
1531   while (*ptr != 0)
1532     {
1533       char *s, *file;
1534       size_t file_len, dir_len;
1535 
1536       file = (char *) ptr;
1537       ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1538       value = read_uleb128 (ptr);
1539 
1540       if (value >= dirt_cnt)
1541 	{
1542 	  error (0, 0, "%s: Wrong directory table index %u",
1543 		 dso->filename, value);
1544 	  return false;
1545 	}
1546       file_len = strlen (file);
1547       if (base_dir && dest_dir)
1548 	{
1549 	  /* Do we need to replace any of the files? Calculate new size. */
1550 	  const char *file_path = skip_dir_prefix (file, base_dir);
1551 	  if (file_path != NULL)
1552 	    {
1553 	      size_t old_size = file_len + 1;
1554 	      size_t file_len = strlen (file_path);
1555 	      size_t new_size = strlen (dest_dir) + 1;
1556 	      if (file_len > 0)
1557 		new_size += 1 + file_len;
1558 	      table->size_diff += (new_size - old_size);
1559 	      table->replace_files = true;
1560 	    }
1561 	}
1562       dir_len = strlen ((char *)dirt[value]);
1563       s = malloc (comp_dir_len + 1 + file_len + 1 + dir_len + 1);
1564       if (s == NULL)
1565 	{
1566 	  error (0, ENOMEM, "%s: Reading file table", dso->filename);
1567 	  return false;
1568 	}
1569       if (*file == '/')
1570 	{
1571 	  memcpy (s, file, file_len + 1);
1572 	}
1573       else if (*dirt[value] == '/')
1574 	{
1575 	  memcpy (s, dirt[value], dir_len);
1576 	  s[dir_len] = '/';
1577 	  memcpy (s + dir_len + 1, file, file_len + 1);
1578 	}
1579       else
1580 	{
1581 	  char *p = s;
1582 	  if (comp_dir_len != 0)
1583 	    {
1584 	      memcpy (s, comp_dir, comp_dir_len);
1585 	      s[comp_dir_len] = '/';
1586 	      p += comp_dir_len + 1;
1587 	    }
1588 	  memcpy (p, dirt[value], dir_len);
1589 	  p[dir_len] = '/';
1590 	  memcpy (p + dir_len + 1, file, file_len + 1);
1591 	}
1592       canonicalize_path (s, s);
1593       if (list_file_fd != -1)
1594 	{
1595 	  const char *p = NULL;
1596 	  if (base_dir == NULL)
1597 	    p = s;
1598 	  else
1599 	    {
1600 	      p = skip_dir_prefix (s, base_dir);
1601 	      if (p == NULL && dest_dir != NULL)
1602 		p = skip_dir_prefix (s, dest_dir);
1603 	    }
1604 
1605 	  if (p)
1606 	    {
1607 	      size_t size = strlen (p) + 1;
1608 	      while (size > 0)
1609 		{
1610 		  ssize_t ret = write (list_file_fd, p, size);
1611 		  if (ret == -1)
1612 		    break;
1613 		  size -= ret;
1614 		  p += ret;
1615 		}
1616 	    }
1617 	}
1618 
1619       free (s);
1620 
1621       read_uleb128 (ptr);
1622       read_uleb128 (ptr);
1623     }
1624 
1625   dso->lines.debug_lines_len += 4 + table->unit_length + table->size_diff;
1626   return table->replace_dirs || table->replace_files;
1627 }
1628 
1629 /* Called during phase one, after the table has been sorted. */
1630 static size_t
find_new_list_offs(struct debug_lines * lines,size_t idx)1631 find_new_list_offs (struct debug_lines *lines, size_t idx)
1632 {
1633   struct line_table key;
1634   key.old_idx = idx;
1635   struct line_table *table = bsearch (&key, lines->table,
1636 				      lines->used,
1637 				      sizeof (struct line_table),
1638 				      line_table_cmp);
1639   return table->new_idx;
1640 }
1641 
1642 /* This scans the attributes of one DIE described by the given abbrev_tag.
1643    PTR points to the data in the debug_info. It will be advanced till all
1644    abbrev data is consumed. In phase zero data is collected, in phase one
1645    data might be replaced/updated.  */
1646 static unsigned char *
edit_attributes(DSO * dso,unsigned char * ptr,struct abbrev_tag * t,int phase)1647 edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
1648 {
1649   int i;
1650   uint32_t list_offs;
1651   int found_list_offs;
1652   char *comp_dir;
1653 
1654   comp_dir = NULL;
1655   list_offs = 0;
1656   found_list_offs = 0;
1657   for (i = 0; i < t->nattr; ++i)
1658     {
1659       uint32_t form = t->attr[i].form;
1660       size_t len = 0;
1661       while (1)
1662 	{
1663 	  /* Whether we already handled a string as file for this
1664 	     attribute.  If we did then we don't need to handle/record
1665 	     it again when handling the DW_FORM_strp later. */
1666 	  bool handled_strp = false;
1667 
1668 	  /* A stmt_list points into the .debug_line section.  In
1669 	     phase zero record all offsets. Then in phase one replace
1670 	     them with the new offsets if we rewrote the line
1671 	     tables.  */
1672 	  if (t->attr[i].attr == DW_AT_stmt_list)
1673 	    {
1674 	      if (form == DW_FORM_data4
1675 		  || form == DW_FORM_sec_offset)
1676 		{
1677 		  list_offs = do_read_32_relocated (ptr);
1678 		  if (phase == 0)
1679 		    found_list_offs = 1;
1680 		  else if (need_stmt_update) /* phase one */
1681 		    {
1682 		      size_t idx, new_idx;
1683 		      idx = do_read_32_relocated (ptr);
1684 		      new_idx = find_new_list_offs (&dso->lines, idx);
1685 		      do_write_32_relocated (ptr, new_idx);
1686 		    }
1687 		}
1688 	    }
1689 
1690 	  /* DW_AT_comp_dir is the current working directory. */
1691 	  if (t->attr[i].attr == DW_AT_comp_dir)
1692 	    {
1693 	      if (form == DW_FORM_string)
1694 		{
1695 		  free (comp_dir);
1696 		  comp_dir = strdup ((char *)ptr);
1697 
1698 		  if (dest_dir)
1699 		    {
1700 		      /* In phase zero we are just collecting dir/file
1701 			 names and check whether any need to be
1702 			 adjusted. If so, in phase one we replace
1703 			 those dir/files.  */
1704 		      const char *file = skip_dir_prefix (comp_dir, base_dir);
1705 		      if (file != NULL && phase == 0)
1706 			need_string_replacement = true;
1707 		      else if (file != NULL && phase == 1)
1708 			{
1709 			  size_t orig_len = strlen (comp_dir);
1710 			  size_t dest_len = strlen (dest_dir);
1711 			  size_t file_len = strlen (file);
1712 			  size_t new_len = dest_len;
1713 			  if (file_len > 0)
1714 			    new_len += 1 + file_len; /* + '/' */
1715 
1716 			  /* We don't want to rewrite the whole
1717 			     debug_info section, so we only replace
1718 			     the comp_dir with something equal or
1719 			     smaller, possibly adding some slashes
1720 			     at the end of the new compdir.  This
1721 			     normally doesn't happen since most
1722 			     producers will use DW_FORM_strp which is
1723 			     more efficient.  */
1724 			  if (orig_len < new_len)
1725 			    fprintf (stderr, "Warning, not replacing comp_dir "
1726 				     "'%s' prefix ('%s' -> '%s') encoded as "
1727 				     "DW_FORM_string. "
1728 				     "Replacement too large.\n",
1729 				     comp_dir, base_dir, dest_dir);
1730 			  else
1731 			    {
1732 			      /* Add zero (if no file part), one or more
1733 				 slashes in between the new dest_dir and the
1734 				 file name to fill up all space (replacement
1735 				 DW_FORM_string must be of the same length).
1736 				 We don't need to copy the old file name (if
1737 				 any) or the zero terminator, because those
1738 				 are already at the end of the string.  */
1739 			      memcpy (ptr, dest_dir, dest_len);
1740 			      memset (ptr + dest_len, '/',
1741 				      orig_len - new_len);
1742 			    }
1743 			}
1744 		    }
1745 		}
1746 	      else if (form == DW_FORM_strp &&
1747 		       debug_sections[DEBUG_STR].data)
1748 		{
1749 		  const char *dir;
1750 		  size_t idx = do_read_32_relocated (ptr);
1751 		  /* In phase zero we collect the comp_dir.  */
1752 		  if (phase == 0)
1753 		    {
1754 		      if (idx >= debug_sections[DEBUG_STR].size)
1755 			error (1, 0,
1756 			       "%s: Bad string pointer index %zd for comp_dir",
1757 			       dso->filename, idx);
1758 		      dir = (char *) debug_sections[DEBUG_STR].data + idx;
1759 
1760 		      free (comp_dir);
1761 		      comp_dir = strdup (dir);
1762 		    }
1763 
1764 		  if (dest_dir != NULL && phase == 0)
1765 		    {
1766 		      if (record_file_string_entry_idx (&dso->strings, idx))
1767 			need_strp_update = true;
1768 		      handled_strp = true;
1769 		    }
1770 		}
1771 	    }
1772 	  else if ((t->tag == DW_TAG_compile_unit
1773 		    || t->tag == DW_TAG_partial_unit)
1774 		   && t->attr[i].attr == DW_AT_name
1775 		   && form == DW_FORM_strp
1776 		   && debug_sections[DEBUG_STR].data)
1777 	    {
1778 	      /* DW_AT_name is the primary file for this compile
1779 		 unit. If starting with / it is a full path name.
1780 		 Note that we don't handle DW_FORM_string in this
1781 		 case.  */
1782 	      size_t idx = do_read_32_relocated (ptr);
1783 
1784 	      /* In phase zero we will look for a comp_dir to use.  */
1785 	      if (phase == 0)
1786 		{
1787 		  if (idx >= debug_sections[DEBUG_STR].size)
1788 		    error (1, 0,
1789 			   "%s: Bad string pointer index %zd for unit name",
1790 			   dso->filename, idx);
1791 		  char *name = (char *) debug_sections[DEBUG_STR].data + idx;
1792 		  if (*name == '/' && comp_dir == NULL)
1793 		    {
1794 		      char *enddir = strrchr (name, '/');
1795 
1796 		      if (enddir != name)
1797 			{
1798 			  comp_dir = malloc (enddir - name + 1);
1799 			  memcpy (comp_dir, name, enddir - name);
1800 			  comp_dir [enddir - name] = '\0';
1801 			}
1802 		      else
1803 			comp_dir = strdup ("/");
1804 		    }
1805 		}
1806 
1807 	      /* First pass (0) records the new name to be
1808 		 added to the debug string pool, the second
1809 		 pass (1) stores it (the new index). */
1810 	      if (dest_dir && phase == 0)
1811 		{
1812 		  if (record_file_string_entry_idx (&dso->strings, idx))
1813 		    need_strp_update = true;
1814 		  handled_strp = true;
1815 		}
1816 	    }
1817 
1818 	  switch (form)
1819 	    {
1820 	    case DW_FORM_ref_addr:
1821 	      if (cu_version == 2)
1822 		ptr += ptr_size;
1823 	      else
1824 		ptr += 4;
1825 	      break;
1826 	    case DW_FORM_flag_present:
1827 	      break;
1828 	    case DW_FORM_addr:
1829 	      ptr += ptr_size;
1830 	      break;
1831 	    case DW_FORM_ref1:
1832 	    case DW_FORM_flag:
1833 	    case DW_FORM_data1:
1834 	      ++ptr;
1835 	      break;
1836 	    case DW_FORM_ref2:
1837 	    case DW_FORM_data2:
1838 	      ptr += 2;
1839 	      break;
1840 	    case DW_FORM_ref4:
1841 	    case DW_FORM_data4:
1842 	    case DW_FORM_sec_offset:
1843 	      ptr += 4;
1844 	      break;
1845 	    case DW_FORM_ref8:
1846 	    case DW_FORM_data8:
1847 	    case DW_FORM_ref_sig8:
1848 	      ptr += 8;
1849 	      break;
1850 	    case DW_FORM_sdata:
1851 	    case DW_FORM_ref_udata:
1852 	    case DW_FORM_udata:
1853 	      read_uleb128 (ptr);
1854 	      break;
1855 	    case DW_FORM_strp:
1856 	      /* In the first pass we collect all strings, in the
1857 		 second we put the new references back (if there are
1858 		 any changes).  */
1859 	      if (phase == 0)
1860 		{
1861 		  /* handled_strp is set for attributes referring to
1862 		     files. If it is set the string is already
1863 		     recorded. */
1864 		  if (! handled_strp)
1865 		    {
1866 		      size_t idx = do_read_32_relocated (ptr);
1867 		      record_existing_string_entry_idx (&dso->strings, idx);
1868 		    }
1869 		}
1870 	      else if (need_strp_update) /* && phase == 1 */
1871 		{
1872 		  struct stridxentry *entry;
1873 		  size_t idx, new_idx;
1874 		  idx = do_read_32_relocated (ptr);
1875 		  entry = string_find_entry (&dso->strings, idx);
1876 		  new_idx = strent_offset (entry->entry);
1877 		  do_write_32_relocated (ptr, new_idx);
1878 		}
1879 	      ptr += 4;
1880 	      break;
1881 	    case DW_FORM_string:
1882 	      ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
1883 	      break;
1884 	    case DW_FORM_indirect:
1885 	      form = read_uleb128 (ptr);
1886 	      continue;
1887 	    case DW_FORM_block1:
1888 	      len = *ptr++;
1889 	      break;
1890 	    case DW_FORM_block2:
1891 	      len = read_16 (ptr);
1892 	      form = DW_FORM_block1;
1893 	      break;
1894 	    case DW_FORM_block4:
1895 	      len = read_32 (ptr);
1896 	      form = DW_FORM_block1;
1897 	      break;
1898 	    case DW_FORM_block:
1899 	    case DW_FORM_exprloc:
1900 	      len = read_uleb128 (ptr);
1901 	      form = DW_FORM_block1;
1902 	      assert (len < UINT_MAX);
1903 	      break;
1904 	    default:
1905 	      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename,
1906 		     form);
1907 	      return NULL;
1908 	    }
1909 
1910 	  if (form == DW_FORM_block1)
1911 	    ptr += len;
1912 
1913 	  break;
1914 	}
1915     }
1916 
1917   /* Ensure the CU current directory will exist even if only empty.  Source
1918      filenames possibly located in its parent directories refer relatively to
1919      it and the debugger (GDB) cannot safely optimize out the missing
1920      CU current dir subdirectories.  Only do this once in phase one. And
1921      only do this for dirs under our build/base_dir.  Don't output the
1922      empty string (in case the comp_dir == base_dir).  */
1923   if (phase == 0 && base_dir && comp_dir && list_file_fd != -1)
1924     {
1925       const char *p = skip_dir_prefix (comp_dir, base_dir);
1926       if (p != NULL && p[0] != '\0')
1927         {
1928 	  size_t size = strlen (p) + 1;
1929 	  while (size > 0)
1930 	    {
1931 	      ssize_t ret = write (list_file_fd, p, size);
1932 	      if (ret == -1)
1933 		break;
1934 	      size -= ret;
1935 	      p += ret;
1936 	    }
1937 	}
1938     }
1939 
1940   /* In phase zero we collect all file names (we need the comp_dir for
1941      that).  Note that calculating the new size and offsets is done
1942      separately (at the end of phase zero after all CUs have been
1943      scanned in dwarf2_edit). */
1944   if (phase == 0 && found_list_offs
1945       && read_dwarf2_line (dso, list_offs, comp_dir))
1946     need_stmt_update = true;
1947 
1948   free (comp_dir);
1949 
1950   return ptr;
1951 }
1952 
1953 static int
line_rel_cmp(const void * a,const void * b)1954 line_rel_cmp (const void *a, const void *b)
1955 {
1956   LINE_REL *rela = (LINE_REL *) a, *relb = (LINE_REL *) b;
1957 
1958   if (rela->r_offset < relb->r_offset)
1959     return -1;
1960 
1961   if (rela->r_offset > relb->r_offset)
1962     return 1;
1963 
1964   return 0;
1965 }
1966 
1967 static int
edit_dwarf2(DSO * dso)1968 edit_dwarf2 (DSO *dso)
1969 {
1970   Elf_Data *data;
1971   Elf_Scn *scn;
1972   int i, j;
1973 
1974   for (i = 0; debug_sections[i].name; ++i)
1975     {
1976       debug_sections[i].data = NULL;
1977       debug_sections[i].size = 0;
1978       debug_sections[i].sec = 0;
1979       debug_sections[i].relsec = 0;
1980     }
1981   ptr_size = 0;
1982 
1983   for (i = 1; i < dso->ehdr.e_shnum; ++i)
1984     if (! (dso->shdr[i].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR))
1985 	&& dso->shdr[i].sh_size)
1986       {
1987         const char *name = strptr (dso, dso->ehdr.e_shstrndx,
1988 				   dso->shdr[i].sh_name);
1989 
1990 	if (strncmp (name, ".debug_", sizeof (".debug_") - 1) == 0)
1991 	  {
1992 	    for (j = 0; debug_sections[j].name; ++j)
1993 	      if (strcmp (name, debug_sections[j].name) == 0)
1994 	 	{
1995 		  struct debug_section *debug_sec = &debug_sections[j];
1996 		  if (debug_sections[j].data)
1997 		    {
1998 		      if (j != DEBUG_MACRO)
1999 			{
2000 			  error (0, 0, "%s: Found two copies of %s section",
2001 				 dso->filename, name);
2002 			  return 1;
2003 			}
2004 		      else
2005 			{
2006 			  /* In relocatable files .debug_macro might
2007 			     appear multiple times as COMDAT
2008 			     section.  */
2009 			  struct debug_section *sec;
2010 			  sec = calloc (sizeof (struct debug_section), 1);
2011 			  if (sec == NULL)
2012 			    error (1, errno,
2013 				   "%s: Could not allocate more macro sections",
2014 				   dso->filename);
2015 			  sec->name = ".debug_macro";
2016 
2017 			  struct debug_section *macro_sec = debug_sec;
2018 			  while (macro_sec->next != NULL)
2019 			    macro_sec = macro_sec->next;
2020 
2021 			  macro_sec->next = sec;
2022 			  debug_sec = sec;
2023 			}
2024 		    }
2025 
2026 		  scn = dso->scn[i];
2027 		  data = elf_getdata (scn, NULL);
2028 		  assert (data != NULL && data->d_buf != NULL);
2029 		  assert (elf_getdata (scn, data) == NULL);
2030 		  assert (data->d_off == 0);
2031 		  assert (data->d_size == dso->shdr[i].sh_size);
2032 		  debug_sec->data = data->d_buf;
2033 		  debug_sec->elf_data = data;
2034 		  debug_sec->size = data->d_size;
2035 		  debug_sec->sec = i;
2036 		  break;
2037 		}
2038 
2039 	    if (debug_sections[j].name == NULL)
2040 	      {
2041 		error (0, 0, "%s: Unknown debugging section %s",
2042 		       dso->filename, name);
2043 	      }
2044 	  }
2045 	else if (dso->ehdr.e_type == ET_REL
2046 		 && ((dso->shdr[i].sh_type == SHT_REL
2047 		      && strncmp (name, ".rel.debug_",
2048 				  sizeof (".rel.debug_") - 1) == 0)
2049 		     || (dso->shdr[i].sh_type == SHT_RELA
2050 			 && strncmp (name, ".rela.debug_",
2051 				     sizeof (".rela.debug_") - 1) == 0)))
2052 	  {
2053 	    for (j = 0; debug_sections[j].name; ++j)
2054 	      if (strcmp (name + sizeof (".rel") - 1
2055 			  + (dso->shdr[i].sh_type == SHT_RELA),
2056 			  debug_sections[j].name) == 0)
2057 	 	{
2058 		  if (j == DEBUG_MACRO)
2059 		    {
2060 		      /* Pick the correct one.  */
2061 		      int rel_target = dso->shdr[i].sh_info;
2062 		      struct debug_section *macro_sec = &debug_sections[j];
2063 		      while (macro_sec != NULL)
2064 			{
2065 			  if (macro_sec->sec == rel_target)
2066 			    {
2067 			      macro_sec->relsec = i;
2068 			      break;
2069 			    }
2070 			  macro_sec = macro_sec->next;
2071 			}
2072 		      if (macro_sec == NULL)
2073 			error (0, 1, "No .debug_macro reloc section: %s",
2074 			       dso->filename);
2075 		    }
2076 		  else
2077 		    debug_sections[j].relsec = i;
2078 		  break;
2079 		}
2080 	  }
2081       }
2082 
2083   if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
2084     {
2085       do_read_16 = buf_read_ule16;
2086       do_read_32 = buf_read_ule32;
2087       do_write_16 = dwarf2_write_le16;
2088       do_write_32 = dwarf2_write_le32;
2089     }
2090   else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
2091     {
2092       do_read_16 = buf_read_ube16;
2093       do_read_32 = buf_read_ube32;
2094       do_write_16 = dwarf2_write_be16;
2095       do_write_32 = dwarf2_write_be32;
2096     }
2097   else
2098     {
2099       error (0, 0, "%s: Wrong ELF data enconding", dso->filename);
2100       return 1;
2101     }
2102 
2103   if (debug_sections[DEBUG_INFO].data != NULL)
2104     {
2105       unsigned char *ptr, *endcu, *endsec;
2106       uint32_t value;
2107       htab_t abbrev;
2108       struct abbrev_tag tag, *t;
2109       int phase;
2110       bool info_rel_updated = false;
2111       bool macro_rel_updated = false;
2112 
2113       for (phase = 0; phase < 2; phase++)
2114 	{
2115 	  /* If we don't need to update anyhing, skip phase 1. */
2116 	  if (phase == 1
2117 	      && !need_strp_update
2118 	      && !need_string_replacement
2119 	      && !need_stmt_update)
2120 	    break;
2121 
2122 	  ptr = debug_sections[DEBUG_INFO].data;
2123 	  setup_relbuf(dso, &debug_sections[DEBUG_INFO], &reltype);
2124 	  rel_updated = false;
2125 	  endsec = ptr + debug_sections[DEBUG_INFO].size;
2126 	  while (ptr < endsec)
2127 	    {
2128 	      if (ptr + 11 > endsec)
2129 		{
2130 		  error (0, 0, "%s: .debug_info CU header too small",
2131 			 dso->filename);
2132 		  return 1;
2133 		}
2134 
2135 	      endcu = ptr + 4;
2136 	      endcu += read_32 (ptr);
2137 	      if (endcu == ptr + 0xffffffff)
2138 		{
2139 		  error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
2140 		  return 1;
2141 		}
2142 
2143 	      if (endcu > endsec)
2144 		{
2145 		  error (0, 0, "%s: .debug_info too small", dso->filename);
2146 		  return 1;
2147 		}
2148 
2149 	      cu_version = read_16 (ptr);
2150 	      if (cu_version != 2 && cu_version != 3 && cu_version != 4)
2151 		{
2152 		  error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
2153 			 cu_version);
2154 		  return 1;
2155 		}
2156 
2157 	      value = read_32_relocated (ptr);
2158 	      if (value >= debug_sections[DEBUG_ABBREV].size)
2159 		{
2160 		  if (debug_sections[DEBUG_ABBREV].data == NULL)
2161 		    error (0, 0, "%s: .debug_abbrev not present", dso->filename);
2162 		  else
2163 		    error (0, 0, "%s: DWARF CU abbrev offset too large",
2164 			   dso->filename);
2165 		  return 1;
2166 		}
2167 
2168 	      if (ptr_size == 0)
2169 		{
2170 		  ptr_size = read_8 (ptr);
2171 		  if (ptr_size != 4 && ptr_size != 8)
2172 		    {
2173 		      error (0, 0, "%s: Invalid DWARF pointer size %d",
2174 			     dso->filename, ptr_size);
2175 		      return 1;
2176 		    }
2177 		}
2178 	      else if (read_8 (ptr) != ptr_size)
2179 		{
2180 		  error (0, 0, "%s: DWARF pointer size differs between CUs",
2181 			 dso->filename);
2182 		  return 1;
2183 		}
2184 
2185 	      abbrev = read_abbrev (dso,
2186 				    debug_sections[DEBUG_ABBREV].data + value);
2187 	      if (abbrev == NULL)
2188 		return 1;
2189 
2190 	      while (ptr < endcu)
2191 		{
2192 		  tag.entry = read_uleb128 (ptr);
2193 		  if (tag.entry == 0)
2194 		    continue;
2195 		  t = htab_find_with_hash (abbrev, &tag, tag.entry);
2196 		  if (t == NULL)
2197 		    {
2198 		      error (0, 0, "%s: Could not find DWARF abbreviation %d",
2199 			     dso->filename, tag.entry);
2200 		      htab_delete (abbrev);
2201 		      return 1;
2202 		    }
2203 
2204 		  ptr = edit_attributes (dso, ptr, t, phase);
2205 		  if (ptr == NULL)
2206 		    break;
2207 		}
2208 
2209 	      htab_delete (abbrev);
2210 	    }
2211 
2212 	  /* Remember whether any .debug_info relocations might need
2213 	     to be updated. */
2214 	  info_rel_updated = rel_updated;
2215 
2216 	  /* We might have to recalculate/rewrite the debug_line
2217 	     section.  We need to do that before going into phase one
2218 	     so we have all new offsets.  We do this separately from
2219 	     scanning the dirs/file names because the DW_AT_stmt_lists
2220 	     might not be in order or skip some padding we might have
2221 	     to (re)move. */
2222 	  if (phase == 0 && need_stmt_update)
2223 	    {
2224 	      edit_dwarf2_line (dso);
2225 
2226 	      /* The line table programs will be moved
2227 		 forward/backwards a bit in the new data. Update the
2228 		 debug_line relocations to the new offsets. */
2229 	      int rndx = debug_sections[DEBUG_LINE].relsec;
2230 	      if (rndx != 0)
2231 		{
2232 		  LINE_REL *rbuf;
2233 		  size_t rels;
2234 		  Elf_Data *rdata = elf_getdata (dso->scn[rndx], NULL);
2235 		  int rtype = dso->shdr[rndx].sh_type;
2236 		  rels = dso->shdr[rndx].sh_size / dso->shdr[rndx].sh_entsize;
2237 		  rbuf = malloc (rels * sizeof (LINE_REL));
2238 		  if (rbuf == NULL)
2239 		    error (1, errno, "%s: Could not allocate line relocations",
2240 			   dso->filename);
2241 
2242 		  /* Sort them by offset into section. */
2243 		  for (size_t i = 0; i < rels; i++)
2244 		    {
2245 		      if (rtype == SHT_RELA)
2246 			{
2247 			  GElf_Rela rela;
2248 			  if (gelf_getrela (rdata, i, &rela) == NULL)
2249 			    error (1, 0, "Couldn't get relocation: %s",
2250 				   elf_errmsg (-1));
2251 			  rbuf[i].r_offset = rela.r_offset;
2252 			  rbuf[i].ndx = i;
2253 			}
2254 		      else
2255 			{
2256 			  GElf_Rel rel;
2257 			  if (gelf_getrel (rdata, i, &rel) == NULL)
2258 			    error (1, 0, "Couldn't get relocation: %s",
2259 				   elf_errmsg (-1));
2260 			  rbuf[i].r_offset = rel.r_offset;
2261 			  rbuf[i].ndx = i;
2262 			}
2263 		    }
2264 		  qsort (rbuf, rels, sizeof (LINE_REL), line_rel_cmp);
2265 
2266 		  size_t lndx = 0;
2267 		  for (size_t i = 0; i < rels; i++)
2268 		    {
2269 		      /* These relocations only happen in ET_REL files
2270 			 and are section offsets. */
2271 		      GElf_Addr r_offset;
2272 		      size_t ndx = rbuf[i].ndx;
2273 
2274 		      GElf_Rel rel;
2275 		      GElf_Rela rela;
2276 		      if (rtype == SHT_RELA)
2277 			{
2278 			  if (gelf_getrela (rdata, ndx, &rela) == NULL)
2279 			    error (1, 0, "Couldn't get relocation: %s",
2280 				   elf_errmsg (-1));
2281 			  r_offset = rela.r_offset;
2282 			}
2283 		      else
2284 			{
2285 			  if (gelf_getrel (rdata, ndx, &rel) == NULL)
2286 			    error (1, 0, "Couldn't get relocation: %s",
2287 				   elf_errmsg (-1));
2288 			  r_offset = rel.r_offset;
2289 			}
2290 
2291 		      while (lndx < dso->lines.used
2292 			     && r_offset > (dso->lines.table[lndx].old_idx
2293 					    + 4
2294 					    + dso->lines.table[lndx].unit_length))
2295 			lndx++;
2296 
2297 		      if (lndx >= dso->lines.used)
2298 			error (1, 0,
2299 			       ".debug_line relocation offset out of range");
2300 
2301 		      /* Offset (pointing into the line program) moves
2302 			 from old to new index including the header
2303 			 size diff. */
2304 		      r_offset += (ssize_t)((dso->lines.table[lndx].new_idx
2305 					     - dso->lines.table[lndx].old_idx)
2306 					    + dso->lines.table[lndx].size_diff);
2307 
2308 		      if (rtype == SHT_RELA)
2309 			{
2310 			  rela.r_offset = r_offset;
2311 			  if (gelf_update_rela (rdata, ndx, &rela) == 0)
2312 			    error (1, 0, "Couldn't update relocation: %s",
2313 				   elf_errmsg (-1));
2314 			}
2315 		      else
2316 			{
2317 			  rel.r_offset = r_offset;
2318 			  if (gelf_update_rel (rdata, ndx, &rel) == 0)
2319 			    error (1, 0, "Couldn't update relocation: %s",
2320 				   elf_errmsg (-1));
2321 			}
2322 		    }
2323 
2324 		  elf_flagdata (rdata, ELF_C_SET, ELF_F_DIRTY);
2325 		  free (rbuf);
2326 		}
2327 	    }
2328 
2329 	  /* The .debug_macro section also contains offsets into the
2330 	     .debug_str section and references to the .debug_line
2331 	     tables, so we need to update those as well if we update
2332 	     the strings or the stmts.  */
2333 	  if ((need_strp_update || need_stmt_update)
2334 	      && debug_sections[DEBUG_MACRO].data)
2335 	    {
2336 	      /* There might be multiple (COMDAT) .debug_macro sections.  */
2337 	      struct debug_section *macro_sec = &debug_sections[DEBUG_MACRO];
2338 	      while (macro_sec != NULL)
2339 		{
2340 		  setup_relbuf(dso, macro_sec, &reltype);
2341 		  rel_updated = false;
2342 
2343 		  ptr = macro_sec->data;
2344 		  endsec = ptr + macro_sec->size;
2345 		  int op = 0, macro_version, macro_flags;
2346 		  int offset_len = 4, line_offset = 0;
2347 
2348 		  while (ptr < endsec)
2349 		    {
2350 		      if (!op)
2351 			{
2352 			  macro_version = read_16 (ptr);
2353 			  macro_flags = read_8 (ptr);
2354 			  if (macro_version < 4 || macro_version > 5)
2355 			    error (1, 0, "unhandled .debug_macro version: %d",
2356 				   macro_version);
2357 			  if ((macro_flags & ~2) != 0)
2358 			    error (1, 0, "unhandled .debug_macro flags: 0x%x",
2359 				   macro_flags);
2360 
2361 			  offset_len = (macro_flags & 0x01) ? 8 : 4;
2362 			  line_offset = (macro_flags & 0x02) ? 1 : 0;
2363 
2364 			  if (offset_len != 4)
2365 			    error (0, 1,
2366 				   "Cannot handle 8 byte macro offsets: %s",
2367 				   dso->filename);
2368 
2369 			  /* Update the line_offset if it is there.  */
2370 			  if (line_offset)
2371 			    {
2372 			      if (phase == 0)
2373 				ptr += offset_len;
2374 			      else
2375 				{
2376 				  size_t idx, new_idx;
2377 				  idx = do_read_32_relocated (ptr);
2378 				  new_idx = find_new_list_offs (&dso->lines,
2379 								idx);
2380 				  write_32_relocated (ptr, new_idx);
2381 				}
2382 			    }
2383 			}
2384 
2385 		      op = read_8 (ptr);
2386 		      if (!op)
2387 			continue;
2388 		      switch(op)
2389 			{
2390 			case DW_MACRO_GNU_define:
2391 			case DW_MACRO_GNU_undef:
2392 			  read_uleb128 (ptr);
2393 			  ptr = ((unsigned char *) strchr ((char *) ptr, '\0')
2394 				 + 1);
2395 			  break;
2396 			case DW_MACRO_GNU_start_file:
2397 			  read_uleb128 (ptr);
2398 			  read_uleb128 (ptr);
2399 			  break;
2400 			case DW_MACRO_GNU_end_file:
2401 			  break;
2402 			case DW_MACRO_GNU_define_indirect:
2403 			case DW_MACRO_GNU_undef_indirect:
2404 			  read_uleb128 (ptr);
2405 			  if (phase == 0)
2406 			    {
2407 			      size_t idx = read_32_relocated (ptr);
2408 			      record_existing_string_entry_idx (&dso->strings,
2409 								idx);
2410 			    }
2411 			  else
2412 			    {
2413 			      struct stridxentry *entry;
2414 			      size_t idx, new_idx;
2415 			      idx = do_read_32_relocated (ptr);
2416 			      entry = string_find_entry (&dso->strings, idx);
2417 			      new_idx = strent_offset (entry->entry);
2418 			      write_32_relocated (ptr, new_idx);
2419 			    }
2420 			  break;
2421 			case DW_MACRO_GNU_transparent_include:
2422 			  ptr += offset_len;
2423 			  break;
2424 			default:
2425 			  error (1, 0, "Unhandled DW_MACRO op 0x%x", op);
2426 			  break;
2427 			}
2428 		    }
2429 
2430 		  if (rel_updated)
2431 		    macro_rel_updated = true;
2432 		  macro_sec = macro_sec->next;
2433 		}
2434 	    }
2435 
2436 	  /* Same for the debug_str section. Make sure everything is
2437 	     in place for phase 1 updating of debug_info
2438 	     references. */
2439 	  if (phase == 0 && need_strp_update)
2440 	    {
2441 	      Strtab *strtab = dso->strings.str_tab;
2442 	      Elf_Data *strdata = debug_sections[DEBUG_STR].elf_data;
2443 	      int strndx = debug_sections[DEBUG_STR].sec;
2444 	      Elf_Scn *strscn = dso->scn[strndx];
2445 
2446 	      /* Out with the old. */
2447 	      strdata->d_size = 0;
2448 	      /* In with the new. */
2449 	      strdata = elf_newdata (strscn);
2450 
2451 	      /* We really should check whether we had enough memory,
2452 		 but the old ebl version will just abort on out of
2453 		 memory... */
2454 	      strtab_finalize (strtab, strdata);
2455 	      debug_sections[DEBUG_STR].size = strdata->d_size;
2456 	      dso->strings.str_buf = strdata->d_buf;
2457 	    }
2458 
2459 	}
2460 
2461       /* After phase 1 we might have rewritten the debug_info with
2462 	 new strp, strings and/or linep offsets.  */
2463       if (need_strp_update || need_string_replacement || need_stmt_update)
2464 	dirty_section (DEBUG_INFO);
2465       if (need_strp_update || need_stmt_update)
2466 	dirty_section (DEBUG_MACRO);
2467       if (need_stmt_update)
2468 	dirty_section (DEBUG_LINE);
2469 
2470       /* Update any relocations addends we might have touched. */
2471       if (info_rel_updated)
2472 	update_rela_data (dso, &debug_sections[DEBUG_INFO]);
2473 
2474       if (macro_rel_updated)
2475 	{
2476 	  struct debug_section *macro_sec = &debug_sections[DEBUG_MACRO];
2477 	  while (macro_sec != NULL)
2478 	    {
2479 	      update_rela_data (dso, macro_sec);
2480 	      macro_sec = macro_sec->next;
2481 	    }
2482 	}
2483     }
2484 
2485   return 0;
2486 }
2487 
2488 static struct poptOption optionsTable[] = {
2489     { "base-dir",  'b', POPT_ARG_STRING, &base_dir, 0,
2490       "base build directory of objects", NULL },
2491     { "dest-dir",  'd', POPT_ARG_STRING, &dest_dir, 0,
2492       "directory to rewrite base-dir into", NULL },
2493     { "list-file",  'l', POPT_ARG_STRING, &list_file, 0,
2494       "file where to put list of source and header file names", NULL },
2495     { "build-id",  'i', POPT_ARG_NONE, &do_build_id, 0,
2496       "recompute build ID note and print ID on stdout", NULL },
2497     { "build-id-seed", 's', POPT_ARG_STRING, &build_id_seed, 0,
2498       "if recomputing the build ID note use this string as hash seed", NULL },
2499     { "no-recompute-build-id",  'n', POPT_ARG_NONE, &no_recompute_build_id, 0,
2500       "do not recompute build ID note even when -i or -s are given", NULL },
2501     { "version", '\0', POPT_ARG_NONE, &show_version, 0,
2502       "print the debugedit version", NULL },
2503       POPT_AUTOHELP
2504     { NULL, 0, 0, NULL, 0, NULL, NULL }
2505 };
2506 
2507 static DSO *
fdopen_dso(int fd,const char * name)2508 fdopen_dso (int fd, const char *name)
2509 {
2510   Elf *elf = NULL;
2511   GElf_Ehdr ehdr;
2512   int i;
2513   DSO *dso = NULL;
2514   size_t phnum;
2515 
2516   elf = elf_begin (fd, ELF_C_RDWR, NULL);
2517   if (elf == NULL)
2518     {
2519       error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1));
2520       goto error_out;
2521     }
2522 
2523   if (elf_kind (elf) != ELF_K_ELF)
2524     {
2525       error (0, 0, "\"%s\" is not an ELF file", name);
2526       goto error_out;
2527     }
2528 
2529   if (gelf_getehdr (elf, &ehdr) == NULL)
2530     {
2531       error (0, 0, "cannot get the ELF header: %s",
2532 	     elf_errmsg (-1));
2533       goto error_out;
2534     }
2535 
2536   if (ehdr.e_type != ET_DYN && ehdr.e_type != ET_EXEC && ehdr.e_type != ET_REL)
2537     {
2538       error (0, 0, "\"%s\" is not a shared library", name);
2539       goto error_out;
2540     }
2541 
2542   /* Allocate DSO structure. Leave place for additional 20 new section
2543      headers.  */
2544   dso = (DSO *)
2545 	malloc (sizeof(DSO) + (ehdr.e_shnum + 20) * sizeof(GElf_Shdr)
2546 	        + (ehdr.e_shnum + 20) * sizeof(Elf_Scn *));
2547   if (!dso)
2548     {
2549       error (0, ENOMEM, "Could not open DSO");
2550       goto error_out;
2551     }
2552 
2553   if (elf_getphdrnum (elf, &phnum) != 0)
2554     {
2555       error (0, 0, "Couldn't get number of phdrs: %s", elf_errmsg (-1));
2556       goto error_out;
2557     }
2558 
2559   /* If there are phdrs we want to maintain the layout of the
2560      allocated sections in the file.  */
2561   if (phnum != 0)
2562     elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
2563 
2564   memset (dso, 0, sizeof(DSO));
2565   dso->elf = elf;
2566   dso->phnum = phnum;
2567   dso->ehdr = ehdr;
2568   dso->scn = (Elf_Scn **) &dso->shdr[ehdr.e_shnum + 20];
2569 
2570   for (i = 0; i < ehdr.e_shnum; ++i)
2571     {
2572       dso->scn[i] = elf_getscn (elf, i);
2573       gelf_getshdr (dso->scn[i], dso->shdr + i);
2574     }
2575 
2576   dso->filename = (const char *) strdup (name);
2577   setup_strings (&dso->strings);
2578   setup_lines (&dso->lines);
2579   return dso;
2580 
2581 error_out:
2582   if (dso)
2583     {
2584       free ((char *) dso->filename);
2585       destroy_strings (&dso->strings);
2586       destroy_lines (&dso->lines);
2587       free (dso);
2588     }
2589   if (elf)
2590     elf_end (elf);
2591   if (fd != -1)
2592     close (fd);
2593   return NULL;
2594 }
2595 
2596 static const pgpHashAlgo algorithms[] = { PGPHASHALGO_MD5,
2597   PGPHASHALGO_SHA1, PGPHASHALGO_SHA256, PGPHASHALGO_SHA384, PGPHASHALGO_SHA512 };
2598 
2599 /* Compute a fresh build ID bit-string from the editted file contents.  */
2600 static void
handle_build_id(DSO * dso,Elf_Data * build_id,size_t build_id_offset,size_t build_id_size)2601 handle_build_id (DSO *dso, Elf_Data *build_id,
2602 		 size_t build_id_offset, size_t build_id_size)
2603 {
2604   DIGEST_CTX ctx;
2605   pgpHashAlgo algorithm;
2606   int i = sizeof(algorithms)/sizeof(algorithms[0]);
2607   void *digest = NULL;
2608   size_t len;
2609 
2610   while (i-- > 0)
2611     {
2612       algorithm = algorithms[i];
2613       if (rpmDigestLength(algorithm) == build_id_size)
2614 	break;
2615     }
2616   if (i < 0)
2617     {
2618       fprintf (stderr, "Cannot handle %Zu-byte build ID\n", build_id_size);
2619       exit (1);
2620     }
2621 
2622   if (no_recompute_build_id
2623       || (! dirty_elf && build_id_seed == NULL))
2624     goto print;
2625 
2626   /* Clear the old bits so they do not affect the new hash.  */
2627   memset ((char *) build_id->d_buf + build_id_offset, 0, build_id_size);
2628 
2629   ctx = rpmDigestInit(algorithm, 0);
2630 
2631   /* If a seed string was given use it to prime the hash.  */
2632   if (build_id_seed != NULL)
2633     rpmDigestUpdate(ctx, build_id_seed, strlen (build_id_seed));
2634 
2635   /* Slurp the relevant header bits and section contents and feed them
2636      into the hash function.  The only bits we ignore are the offset
2637      fields in ehdr and shdrs, since the semantically identical ELF file
2638      could be written differently if it doesn't change the phdr layout.
2639      We always use the GElf (i.e. Elf64) formats for the bits to hash
2640      since it is convenient.  It doesn't matter whether this is an Elf32
2641      or Elf64 object, only that we are consistent in what bits feed the
2642      hash so it comes out the same for the same file contents.  */
2643   {
2644     union
2645     {
2646       GElf_Ehdr ehdr;
2647       GElf_Phdr phdr;
2648       GElf_Shdr shdr;
2649     } u;
2650     Elf_Data x = { .d_version = EV_CURRENT, .d_buf = &u };
2651 
2652     x.d_type = ELF_T_EHDR;
2653     x.d_size = sizeof u.ehdr;
2654     u.ehdr = dso->ehdr;
2655     u.ehdr.e_phoff = u.ehdr.e_shoff = 0;
2656     if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
2657       {
2658       bad:
2659 	fprintf (stderr, "Failed to compute header checksum: %s\n",
2660 		 elf_errmsg (elf_errno ()));
2661 	exit (1);
2662       }
2663 
2664     x.d_type = ELF_T_PHDR;
2665     x.d_size = sizeof u.phdr;
2666     for (i = 0; i < dso->ehdr.e_phnum; ++i)
2667       {
2668 	if (gelf_getphdr (dso->elf, i, &u.phdr) == NULL)
2669 	  goto bad;
2670 	if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
2671 	  goto bad;
2672 	rpmDigestUpdate(ctx, x.d_buf, x.d_size);
2673       }
2674 
2675     x.d_type = ELF_T_SHDR;
2676     x.d_size = sizeof u.shdr;
2677     for (i = 0; i < dso->ehdr.e_shnum; ++i)
2678       if (dso->scn[i] != NULL)
2679 	{
2680 	  u.shdr = dso->shdr[i];
2681 	  u.shdr.sh_offset = 0;
2682 	  if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
2683 	    goto bad;
2684 	  rpmDigestUpdate(ctx, x.d_buf, x.d_size);
2685 
2686 	  if (u.shdr.sh_type != SHT_NOBITS)
2687 	    {
2688 	      Elf_Data *d = elf_getdata (dso->scn[i], NULL);
2689 	      if (d == NULL)
2690 		goto bad;
2691 	      rpmDigestUpdate(ctx, d->d_buf, d->d_size);
2692 	    }
2693 	}
2694   }
2695 
2696   rpmDigestFinal(ctx, &digest, &len, 0);
2697   memcpy((unsigned char *)build_id->d_buf + build_id_offset, digest, build_id_size);
2698   free(digest);
2699 
2700   elf_flagdata (build_id, ELF_C_SET, ELF_F_DIRTY);
2701 
2702  print:
2703   /* Now format the build ID bits in hex to print out.  */
2704   {
2705     const uint8_t * id = (uint8_t *)build_id->d_buf + build_id_offset;
2706     char *hex = pgpHexStr(id, build_id_size);
2707     puts (hex);
2708     free(hex);
2709   }
2710 }
2711 
2712 int
main(int argc,char * argv[])2713 main (int argc, char *argv[])
2714 {
2715   DSO *dso;
2716   int fd, i;
2717   const char *file;
2718   poptContext optCon;   /* context for parsing command-line options */
2719   int nextopt;
2720   const char **args;
2721   struct stat stat_buf;
2722   Elf_Data *build_id = NULL;
2723   size_t build_id_offset = 0, build_id_size = 0;
2724 
2725   optCon = poptGetContext("debugedit", argc, (const char **)argv, optionsTable, 0);
2726 
2727   while ((nextopt = poptGetNextOpt (optCon)) > 0 || nextopt == POPT_ERROR_BADOPT)
2728     /* do nothing */ ;
2729 
2730   if (nextopt != -1)
2731     {
2732       fprintf (stderr, "Error on option %s: %s.\nRun '%s --help' to see a full list of available command line options.\n",
2733 	      poptBadOption (optCon, 0),
2734 	      poptStrerror (nextopt),
2735 	      argv[0]);
2736       exit (1);
2737     }
2738 
2739   if (show_version)
2740     {
2741       printf("RPM debugedit %s\n", VERSION);
2742       exit(EXIT_SUCCESS);
2743     }
2744 
2745   args = poptGetArgs (optCon);
2746   if (args == NULL || args[0] == NULL || args[1] != NULL)
2747     {
2748       poptPrintHelp(optCon, stdout, 0);
2749       exit (1);
2750     }
2751 
2752   if (dest_dir != NULL)
2753     {
2754       if (base_dir == NULL)
2755 	{
2756 	  fprintf (stderr, "You must specify a base dir if you specify a dest dir\n");
2757 	  exit (1);
2758 	}
2759     }
2760 
2761   if (build_id_seed != NULL && do_build_id == 0)
2762     {
2763       fprintf (stderr, "--build-id-seed (-s) needs --build-id (-i)\n");
2764       exit (1);
2765     }
2766 
2767   if (build_id_seed != NULL && strlen (build_id_seed) < 1)
2768     {
2769       fprintf (stderr,
2770 	       "--build-id-seed (-s) string should be at least 1 char\n");
2771       exit (1);
2772     }
2773 
2774   /* Ensure clean paths, users can muck with these. Also removes any
2775      trailing '/' from the paths. */
2776   if (base_dir)
2777     canonicalize_path(base_dir, base_dir);
2778   if (dest_dir)
2779     canonicalize_path(dest_dir, dest_dir);
2780 
2781   if (list_file != NULL)
2782     {
2783       list_file_fd = open (list_file, O_WRONLY|O_CREAT|O_APPEND, 0644);
2784     }
2785 
2786   file = args[0];
2787 
2788   if (elf_version(EV_CURRENT) == EV_NONE)
2789     {
2790       fprintf (stderr, "library out of date\n");
2791       exit (1);
2792     }
2793 
2794   if (stat(file, &stat_buf) < 0)
2795     {
2796       fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno));
2797       exit (1);
2798     }
2799 
2800   /* Make sure we can read and write */
2801   chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR);
2802 
2803   fd = open (file, O_RDWR);
2804   if (fd < 0)
2805     {
2806       fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno));
2807       exit (1);
2808     }
2809 
2810   dso = fdopen_dso (fd, file);
2811   if (dso == NULL)
2812     exit (1);
2813 
2814   for (i = 1; i < dso->ehdr.e_shnum; i++)
2815     {
2816       const char *name;
2817 
2818       switch (dso->shdr[i].sh_type)
2819 	{
2820 	case SHT_PROGBITS:
2821 	  name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name);
2822 	  /* TODO: Handle stabs */
2823 	  if (strcmp (name, ".stab") == 0)
2824 	    {
2825 	      fprintf (stderr, "Stabs debuginfo not supported: %s\n", file);
2826 	      break;
2827 	    }
2828 	  if (!(do_build_id && no_recompute_build_id && !base_dir && !dest_dir)
2829 	      && strcmp (name, ".debug_info") == 0)
2830 	    edit_dwarf2 (dso);
2831 
2832 	  break;
2833 	case SHT_NOTE:
2834 	  if (do_build_id
2835 	      && build_id == 0 && (dso->shdr[i].sh_flags & SHF_ALLOC))
2836 	    {
2837 	      /* Look for a build-ID note here.  */
2838 	      size_t off = 0;
2839 	      GElf_Nhdr nhdr;
2840 	      size_t name_off;
2841 	      size_t desc_off;
2842 	      Elf_Data *data = elf_getdata (elf_getscn (dso->elf, i), NULL);
2843 	      while ((off = gelf_getnote (data, off,
2844 					  &nhdr, &name_off, &desc_off)) > 0)
2845 		if (nhdr.n_type == NT_GNU_BUILD_ID
2846 		    && nhdr.n_namesz == sizeof "GNU"
2847 		    && (memcmp ((char *)data->d_buf + name_off, "GNU",
2848 				sizeof "GNU") == 0))
2849 		  {
2850 		    build_id = data;
2851 		    build_id_offset = desc_off;
2852 		    build_id_size = nhdr.n_descsz;
2853 		  }
2854 	    }
2855 	  break;
2856 	default:
2857 	  break;
2858 	}
2859     }
2860 
2861   /* Normally we only need to explicitly update the section headers
2862      and data when any section data changed size. But because of a bug
2863      in elfutils before 0.169 we will have to update and write out all
2864      section data if any data has changed (when ELF_F_LAYOUT was
2865      set). https://sourceware.org/bugzilla/show_bug.cgi?id=21199 */
2866   bool need_update = need_strp_update || need_stmt_update;
2867 
2868 #if !_ELFUTILS_PREREQ (0, 169)
2869   /* string replacements or build_id updates don't change section size. */
2870   need_update = (need_update
2871 		 || need_string_replacement
2872 		 || (do_build_id && build_id != NULL));
2873 #endif
2874 
2875   /* We might have changed the size of some debug sections. If so make
2876      sure the section headers are updated and the data offsets are
2877      correct. We set ELF_F_LAYOUT above because we don't want libelf
2878      to move any allocated sections around itself if there are any
2879      phdrs. Which means we are responsible for setting the section size
2880      and offset fields. Plus the shdr offsets. We don't want to change
2881      anything for the phdrs allocated sections. Keep the offset of
2882      allocated sections so they are at the same place in the file. Add
2883      unallocated ones after the allocated ones. */
2884   if (dso->phnum != 0 && need_update)
2885     {
2886       Elf *elf = dso->elf;
2887       GElf_Off last_offset;
2888       /* We position everything after the phdrs (which normally would
2889 	 be at the start of the ELF file after the ELF header. */
2890       last_offset = (dso->ehdr.e_phoff + gelf_fsize (elf, ELF_T_PHDR,
2891 						     dso->phnum, EV_CURRENT));
2892 
2893       /* First find the last allocated section.  */
2894       Elf_Scn *scn = NULL;
2895       while ((scn = elf_nextscn (elf, scn)) != NULL)
2896 	{
2897 	  GElf_Shdr shdr_mem;
2898 	  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2899 	  if (shdr == NULL)
2900 	    error (1, 0, "Couldn't get shdr: %s\n", elf_errmsg (-1));
2901 
2902 	  /* Any sections we have changed aren't allocated sections,
2903 	     so we don't need to lookup any changed section sizes. */
2904 	  if ((shdr->sh_flags & SHF_ALLOC) != 0)
2905 	    {
2906 	      GElf_Off off = shdr->sh_offset + (shdr->sh_type != SHT_NOBITS
2907 						? shdr->sh_size : 0);
2908 	      if (last_offset < off)
2909 		last_offset = off;
2910 	    }
2911 	}
2912 
2913       /* Now adjust any sizes and offsets for the unallocated sections. */
2914       scn = NULL;
2915       while ((scn = elf_nextscn (elf, scn)) != NULL)
2916 	{
2917 	  GElf_Shdr shdr_mem;
2918 	  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2919 	  if (shdr == NULL)
2920 	    error (1, 0, "Couldn't get shdr: %s\n", elf_errmsg (-1));
2921 
2922 	  /* A bug in elfutils before 0.169 means we have to write out
2923 	     all section data, even when nothing changed.
2924 	     https://sourceware.org/bugzilla/show_bug.cgi?id=21199 */
2925 #if !_ELFUTILS_PREREQ (0, 169)
2926 	  if (shdr->sh_type != SHT_NOBITS)
2927 	    {
2928 	      Elf_Data *d = elf_getdata (scn, NULL);
2929 	      elf_flagdata (d, ELF_C_SET, ELF_F_DIRTY);
2930 	    }
2931 #endif
2932 	  if ((shdr->sh_flags & SHF_ALLOC) == 0)
2933 	    {
2934 	      GElf_Off sec_offset = shdr->sh_offset;
2935 	      GElf_Xword sec_size = shdr->sh_size;
2936 
2937 	      /* We might have changed the size (and content) of the
2938 		 debug_str or debug_line section. */
2939 	      size_t secnum = elf_ndxscn (scn);
2940 	      if (secnum == debug_sections[DEBUG_STR].sec)
2941 		sec_size = debug_sections[DEBUG_STR].size;
2942 	      if (secnum == debug_sections[DEBUG_LINE].sec)
2943 		sec_size = debug_sections[DEBUG_LINE].size;
2944 
2945 	      /* Zero means one.  No alignment constraints.  */
2946 	      size_t addralign = shdr->sh_addralign ?: 1;
2947 	      last_offset = (last_offset + addralign - 1) & ~(addralign - 1);
2948 	      sec_offset = last_offset;
2949 	      if (shdr->sh_type != SHT_NOBITS)
2950 		last_offset += sec_size;
2951 
2952 	      if (shdr->sh_size != sec_size
2953 		  || shdr->sh_offset != sec_offset)
2954 		{
2955 		  /* Make sure unchanged section data is written out
2956 		     at the new location. */
2957 		  if (shdr->sh_offset != sec_offset
2958 		      && shdr->sh_type != SHT_NOBITS)
2959 		    {
2960 		      Elf_Data *d = elf_getdata (scn, NULL);
2961 		      elf_flagdata (d, ELF_C_SET, ELF_F_DIRTY);
2962 		    }
2963 
2964 		  shdr->sh_size = sec_size;
2965 		  shdr->sh_offset = sec_offset;
2966 		  if (gelf_update_shdr (scn, shdr) == 0)
2967 		    error (1, 0, "Couldn't update shdr: %s\n",
2968 			   elf_errmsg (-1));
2969 		}
2970 	    }
2971 	}
2972 
2973       /* Position the shdrs after the last (unallocated) section.  */
2974       const size_t offsize = gelf_fsize (elf, ELF_T_OFF, 1, EV_CURRENT);
2975       GElf_Off new_offset = ((last_offset + offsize - 1)
2976 			     & ~((GElf_Off) (offsize - 1)));
2977       if (dso->ehdr.e_shoff != new_offset)
2978 	{
2979 	  dso->ehdr.e_shoff = new_offset;
2980 	  if (gelf_update_ehdr (elf, &dso->ehdr) == 0)
2981 	    error (1, 0, "Couldn't update ehdr: %s\n", elf_errmsg (-1));
2982 	}
2983     }
2984 
2985   if (elf_update (dso->elf, ELF_C_NULL) < 0)
2986     {
2987       fprintf (stderr, "Failed to update file: %s\n",
2988 	       elf_errmsg (elf_errno ()));
2989       exit (1);
2990     }
2991 
2992   if (do_build_id && build_id != NULL)
2993     handle_build_id (dso, build_id, build_id_offset, build_id_size);
2994 
2995   if (elf_update (dso->elf, ELF_C_WRITE) < 0)
2996     {
2997       fprintf (stderr, "Failed to write file: %s\n", elf_errmsg (elf_errno()));
2998       exit (1);
2999     }
3000   if (elf_end (dso->elf) < 0)
3001     {
3002       fprintf (stderr, "elf_end failed: %s\n", elf_errmsg (elf_errno()));
3003       exit (1);
3004     }
3005   close (fd);
3006 
3007   /* Restore old access rights */
3008   chmod (file, stat_buf.st_mode);
3009 
3010   free ((char *) dso->filename);
3011   destroy_strings (&dso->strings);
3012   destroy_lines (&dso->lines);
3013   free (dso);
3014 
3015   /* In case there were multiple (COMDAT) .debug_macro sections,
3016      free them.  */
3017   struct debug_section *macro_sec = &debug_sections[DEBUG_MACRO];
3018   macro_sec = macro_sec->next;
3019   while (macro_sec != NULL)
3020     {
3021       struct debug_section *next = macro_sec->next;
3022       free (macro_sec);
3023       macro_sec = next;
3024     }
3025 
3026   poptFreeContext (optCon);
3027 
3028   return 0;
3029 }
3030