forked from CoryXie/DebugEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugedit.c
1819 lines (1569 loc) · 53.2 KB
/
debugedit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2001, 2002, 2003, 2005, 2007, 2009, 2010 Red Hat, Inc.
Written by Alexander Larsson <[email protected]>, 2002
Based on code by Jakub Jelinek <[email protected]>, 2001.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Needed for libelf */
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#if defined(__linux__)
#include <byteswap.h>
#include <endian.h>
#endif
#include <errno.h>
#if !defined(__FreeBSD__)
#include <error.h>
#else
#include <err.h>
#define error(x, y, format, args...) errx(1, format, ## args)
#endif
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <popt.h>
#include <gelf.h>
#include <elf.h>
#include "dwarf.h"
#include "hashtab.h"
#define DW_TAG_partial_unit 0x3c
#define DW_FORM_sec_offset 0x17
#define DW_FORM_exprloc 0x18
#define DW_FORM_flag_present 0x19
#define DW_FORM_ref_sig8 0x20
#if !defined(R_390_32)
#define R_390_32 0x04
#endif
#if !defined(R_IA64_SECREL32LSB)
#define R_IA64_SECREL32LSB 0x65
#endif
char *base_dir = NULL;
char *dest_dir = NULL;
char *list_file = NULL;
int win_path = 0;
int list_file_fd = -1;
int use_newline = 0;
int list_only_files = 0;
FILE *debug_fd;
int be_quiet = 0;
typedef struct
{
Elf *elf;
GElf_Ehdr ehdr;
Elf_Scn **scn;
const char *filename;
int lastscn;
GElf_Shdr shdr[0];
} DSO;
typedef struct
{
unsigned char *ptr;
uint32_t addend;
} REL;
#define read_uleb128(ptr) ({ \
unsigned int ret = 0; \
unsigned int c; \
int shift = 0; \
do \
{ \
c = *ptr++; \
ret |= (c & 0x7f) << shift; \
shift += 7; \
} while (c & 0x80); \
\
if (shift >= 35) \
ret = UINT_MAX; \
ret; \
})
static uint16_t (*do_read_16) (unsigned char *ptr);
static uint32_t (*do_read_32) (unsigned char *ptr);
static void (*write_32) (unsigned char *ptr, GElf_Addr val);
static int ptr_size;
static int cu_version;
static inline uint16_t
buf_read_ule16 (unsigned char *data)
{
return data[0] | (data[1] << 8);
}
static inline uint16_t
buf_read_ube16 (unsigned char *data)
{
return data[1] | (data[0] << 8);
}
static inline uint32_t
buf_read_ule32 (unsigned char *data)
{
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}
static inline uint32_t
buf_read_ube32 (unsigned char *data)
{
return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
}
static const char *
strptr (DSO *dso, int sec, off_t offset)
{
Elf_Scn *scn;
Elf_Data *data;
scn = dso->scn[sec];
if (offset >= 0 && (GElf_Addr) offset < dso->shdr[sec].sh_size)
{
data = NULL;
while ((data = elf_getdata (scn, data)) != NULL)
{
if (data->d_buf
&& offset >= data->d_off
&& offset < data->d_off + data->d_size)
return (const char *) data->d_buf + (offset - data->d_off);
}
}
return NULL;
}
#define read_1(ptr) *ptr++
#define read_16(ptr) ({ \
uint16_t ret = do_read_16 (ptr); \
ptr += 2; \
ret; \
})
#define read_32(ptr) ({ \
uint32_t ret = do_read_32 (ptr); \
ptr += 4; \
ret; \
})
REL *relptr, *relend;
int reltype;
#define do_read_32_relocated(ptr) ({ \
uint32_t dret = do_read_32 (ptr); \
if (relptr) \
{ \
while (relptr < relend && relptr->ptr < ptr) \
++relptr; \
if (relptr < relend && relptr->ptr == ptr) \
{ \
if (reltype == SHT_REL) \
dret += relptr->addend; \
else \
dret = relptr->addend; \
} \
} \
dret; \
})
#define read_32_relocated(ptr) ({ \
uint32_t ret = do_read_32_relocated (ptr); \
ptr += 4; \
ret; \
})
static void
dwarf2_write_le32 (unsigned char *p, GElf_Addr val)
{
uint32_t v = (uint32_t) val;
p[0] = v;
p[1] = v >> 8;
p[2] = v >> 16;
p[3] = v >> 24;
}
static void
dwarf2_write_be32 (unsigned char *p, GElf_Addr val)
{
uint32_t v = (uint32_t) val;
p[3] = v;
p[2] = v >> 8;
p[1] = v >> 16;
p[0] = v >> 24;
}
static struct
{
const char *name;
unsigned char *data;
Elf_Data *elf_data;
size_t size;
int sec, relsec;
} debug_sections[] =
{
#define DEBUG_INFO 0
#define DEBUG_ABBREV 1
#define DEBUG_LINE 2
#define DEBUG_ARANGES 3
#define DEBUG_PUBNAMES 4
#define DEBUG_PUBTYPES 5
#define DEBUG_MACINFO 6
#define DEBUG_LOC 7
#define DEBUG_STR 8
#define DEBUG_FRAME 9
#define DEBUG_RANGES 10
#define DEBUG_TYPES 11
#define DEBUG_MACRO 12
#define DEBUG_GDB_SCRIPT 13
#define DEBUG_SYMTAB 14
{ ".debug_info", NULL, NULL, 0, 0, 0 },
{ ".debug_abbrev", NULL, NULL, 0, 0, 0 },
{ ".debug_line", NULL, NULL, 0, 0, 0 },
{ ".debug_aranges", NULL, NULL, 0, 0, 0 },
{ ".debug_pubnames", NULL, NULL, 0, 0, 0 },
{ ".debug_pubtypes", NULL, NULL, 0, 0, 0 },
{ ".debug_macinfo", NULL, NULL, 0, 0, 0 },
{ ".debug_loc", NULL, NULL, 0, 0, 0 },
{ ".debug_str", NULL, NULL, 0, 0, 0 },
{ ".debug_frame", NULL, NULL, 0, 0, 0 },
{ ".debug_ranges", NULL, NULL, 0, 0, 0 },
{ ".debug_types", NULL, NULL, 0, 0, 0 },
{ ".debug_macro", NULL, NULL, 0, 0, 0 },
{ ".debug_gdb_scripts", NULL, NULL, 0, 0, 0 },
{ ".symtab", NULL, NULL, 0, 0, 0 },
{ NULL, NULL, NULL, 0, 0, 0 }
};
struct abbrev_attr
{
unsigned int attr;
unsigned int form;
};
struct abbrev_tag
{
unsigned int entry;
unsigned int tag;
int nattr;
struct abbrev_attr attr[0];
};
static hashval_t
abbrev_hash (const void *p)
{
struct abbrev_tag *t = (struct abbrev_tag *)p;
return t->entry;
}
static int
abbrev_eq (const void *p, const void *q)
{
struct abbrev_tag *t1 = (struct abbrev_tag *)p;
struct abbrev_tag *t2 = (struct abbrev_tag *)q;
return t1->entry == t2->entry;
}
static void
abbrev_del (void *p)
{
free (p);
}
static htab_t
read_abbrev (DSO *dso, unsigned char *ptr)
{
htab_t h = htab_try_create (50, abbrev_hash, abbrev_eq, abbrev_del);
unsigned int attr, form;
struct abbrev_tag *t;
int size;
void **slot;
if (h == NULL)
{
no_memory:
error (0, ENOMEM, "%s: Could not read .debug_abbrev", dso->filename);
if (h)
htab_delete (h);
return NULL;
}
while ((attr = read_uleb128 (ptr)) != 0)
{
size = 10;
t = malloc (sizeof (*t) + size * sizeof (struct abbrev_attr));
if (t == NULL)
goto no_memory;
t->entry = attr;
t->nattr = 0;
slot = htab_find_slot (h, t, INSERT);
if (slot == NULL)
{
free (t);
goto no_memory;
}
if (*slot != NULL)
{
error (0, 0, "%s: Duplicate DWARF abbreviation %d", dso->filename,
t->entry);
free (t);
htab_delete (h);
return NULL;
}
t->tag = read_uleb128 (ptr);
++ptr; /* skip children flag. */
while ((attr = read_uleb128 (ptr)) != 0)
{
if (t->nattr == size)
{
size += 10;
t = realloc (t, sizeof (*t) + size * sizeof (struct abbrev_attr));
if (t == NULL)
goto no_memory;
}
form = read_uleb128 (ptr);
if (form == 2
|| (form > DW_FORM_flag_present && form != DW_FORM_ref_sig8))
{
error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename, form);
htab_delete (h);
return NULL;
}
t->attr[t->nattr].attr = attr;
t->attr[t->nattr++].form = form;
}
if (read_uleb128 (ptr) != 0)
{
error (0, 0, "%s: DWARF abbreviation does not end with 2 zeros",
dso->filename);
htab_delete (h);
return NULL;
}
*slot = t;
}
return h;
}
#define IS_DIR_SEPARATOR(c) ((c)=='/')
static char *
canonicalize_path (const char *s, char *d)
{
char *rv = d;
char *droot;
if (IS_DIR_SEPARATOR (*s))
{
*d++ = *s++;
if (IS_DIR_SEPARATOR (*s) && !IS_DIR_SEPARATOR (s[1]))
{
/* Special case for "//foo" meaning a Posix namespace
escape. */
*d++ = *s++;
}
while (IS_DIR_SEPARATOR (*s))
s++;
}
droot = d;
while (*s)
{
/* At this point, we're always at the beginning of a path
segment. */
if (s[0] == '.' && (s[1] == 0 || IS_DIR_SEPARATOR (s[1])))
{
s++;
if (*s)
while (IS_DIR_SEPARATOR (*s))
++s;
}
else if (s[0] == '.' && s[1] == '.'
&& (s[2] == 0 || IS_DIR_SEPARATOR (s[2])))
{
char *pre = d - 1; /* includes slash */
while (droot < pre && IS_DIR_SEPARATOR (*pre))
pre--;
if (droot <= pre && ! IS_DIR_SEPARATOR (*pre))
{
while (droot < pre && ! IS_DIR_SEPARATOR (*pre))
pre--;
/* pre now points to the slash */
if (droot < pre)
pre++;
if (pre + 3 == d && pre[0] == '.' && pre[1] == '.')
{
*d++ = *s++;
*d++ = *s++;
}
else
{
d = pre;
s += 2;
if (*s)
while (IS_DIR_SEPARATOR (*s))
s++;
}
}
else
{
*d++ = *s++;
*d++ = *s++;
}
}
else
{
while (*s && ! IS_DIR_SEPARATOR (*s))
*d++ = *s++;
}
if (IS_DIR_SEPARATOR (*s))
{
*d++ = *s++;
while (IS_DIR_SEPARATOR (*s))
s++;
}
}
while (droot < d && IS_DIR_SEPARATOR (d[-1]))
--d;
if (d == rv)
*d++ = '.';
*d = 0;
return rv;
}
static int
has_prefix (const char *str,
const char *prefix)
{
size_t str_len;
size_t prefix_len;
str_len = strlen (str);
prefix_len = strlen (prefix);
if (str_len < prefix_len)
return 0;
return strncmp (str, prefix, prefix_len) == 0;
}
static int dirty_elf;
static void
dirty_section (unsigned int sec)
{
elf_flagdata (debug_sections[sec].elf_data, ELF_C_SET, ELF_F_DIRTY);
dirty_elf = 1;
}
void make_win_path(char * path)
{
int k = 0;
while (path[k] != '\0')
{
if (path[k] == '/') path[k] = '\\';
k++;
}
}
#define LST_FILE 0
#define LST_DIR 1
static int
append_list_file(char *p, int type)
{
size_t size = strlen (p) + 1;
ssize_t ret;
if (list_only_files != 0 && type != LST_FILE)
return (0);
ret = 0;
if (use_newline != 0)
p[size - 1] = '\n';
while (size > 0)
{
ret = write (list_file_fd, p, size);
if (ret == -1)
break;
size -= ret;
p += ret;
}
if (use_newline != 0)
p[size - 1] = '\0';
return (ret < 0 ? -1 : 0);
}
static int
edit_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir, int phase)
{
unsigned char *ptr = debug_sections[DEBUG_LINE].data, *dir;
unsigned char **dirt;
unsigned char *endsec = ptr + debug_sections[DEBUG_LINE].size;
unsigned char *endcu, *endprol;
unsigned char opcode_base;
uint32_t value, dirt_cnt;
size_t comp_dir_len = strlen (comp_dir);
size_t abs_file_cnt = 0, abs_dir_cnt = 0;
if (phase != 0)
return 0;
/* XXX: RhBug:929365, should we error out instead of ignoring? */
if (ptr == NULL)
return 0;
ptr += off;
/*
* unit_length
*
* The size in bytes of the line number information for this
* compilation unit, not including the unit_length field itself.
*/
endcu = ptr + 4;
endcu += read_32 (ptr);
if (endcu == ptr + 0xffffffff)
{
error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
return 1;
}
if (endcu > endsec)
{
error (0, 0, "%s: .debug_line CU does not fit into section",
dso->filename);
return 1;
}
/*
* version
*
* A version number. This number is specific to the line number
* information and is independent of the DWARF version number.
*/
value = read_16 (ptr);
if (value != 2 && value != 3 && value != 4)
{
error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
value);
return 1;
}
/*
* header_length
*
* The number of bytes following the header_length field to the
* beginning of the first byte of the line number program itself.
* In the 32-bit DWARF format, this is a 4-byte unsigned length;
* in the 64-bit DWARF format, this field is an 8-byte unsigned
* length (see Section 7.4).
*/
endprol = ptr + 4;
endprol += read_32 (ptr);
if (endprol > endcu)
{
error (0, 0, "%s: .debug_line CU prologue does not fit into CU",
dso->filename);
return 1;
}
opcode_base = ptr[4 + (value >= 4)];
ptr = dir = ptr + 4 + (value >= 4) + opcode_base;
/*
* include_directories (sequence of path names)
*
* Entries in this sequence describe each path that was searched
* for included source files in this compilation.
*
* The last entry is followed by a single null byte.
*/
/* dir table: */
value = 1;
while (*ptr != 0)
{
ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
++value;
}
dirt = (unsigned char **) alloca (value * sizeof (unsigned char *));
dirt[0] = (unsigned char *) ".";
dirt_cnt = 1;
ptr = dir;
while (*ptr != 0)
{
dirt[dirt_cnt++] = ptr;
ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
}
ptr++;
/* file table: */
while (*ptr != 0)
{
char *s, *file;
size_t file_len, dir_len;
file = (char *) ptr;
ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
value = read_uleb128 (ptr);
if (value >= dirt_cnt)
{
error (0, 0, "%s: Wrong directory table index %u",
dso->filename, value);
return 1;
}
/* GCC emits those it seems sometimes */
if (strcmp(file, "<built-in>") == 0)
goto skip;
file_len = strlen (file);
dir_len = strlen ((char *)dirt[value]);
s = malloc (comp_dir_len + 1 + file_len + 1 + dir_len + 1);
if (s == NULL)
{
error (0, ENOMEM, "%s: Reading file table", dso->filename);
return 1;
}
if (*file == '/')
{
memcpy (s, file, file_len + 1);
if (dest_dir && has_prefix (file, base_dir))
++abs_file_cnt;
}
else if (*dirt[value] == '/')
{
memcpy (s, dirt[value], dir_len);
s[dir_len] = '/';
memcpy (s + dir_len + 1, file, file_len + 1);
}
else
{
char *p = s;
if (comp_dir_len != 0)
{
memcpy (s, comp_dir, comp_dir_len);
s[comp_dir_len] = '/';
p += comp_dir_len + 1;
}
memcpy (p, dirt[value], dir_len);
p[dir_len] = '/';
memcpy (p + dir_len + 1, file, file_len + 1);
}
fprintf(debug_fd, "@@@@linedirt[%d] %s\n", value, dirt[value]);
canonicalize_path (s, s);
if (list_file_fd != -1)
{
char *p = NULL;
if (base_dir == NULL)
p = s;
else if (has_prefix (s, base_dir))
p = s + strlen (base_dir);
else if (has_prefix (s, dest_dir))
p = s + strlen (dest_dir);
if (p)
{
append_list_file(p, LST_FILE);
}
}
if (win_path)
make_win_path(s);
free (s);
skip:
read_uleb128 (ptr);
read_uleb128 (ptr);
}
++ptr;
if (dest_dir)
{
unsigned char *srcptr, *buf = NULL;
size_t base_len = strlen (base_dir);
size_t dest_len = strlen (dest_dir);
size_t shrank = 0;
if (dest_len == base_len)
abs_file_cnt = 0;
if (abs_file_cnt)
{
srcptr = buf = malloc (ptr - dir);
memcpy (srcptr, dir, ptr - dir);
ptr = dir;
}
else
ptr = srcptr = dir;
while (*srcptr != 0)
{
size_t len = strlen ((char *)srcptr) + 1;
const unsigned char *readptr = srcptr;
char *orig = strdup ((const char *) srcptr);
fprintf(debug_fd, "####linesrcptr %s\n", srcptr);
if (*srcptr == '/' && has_prefix ((char *)srcptr, base_dir))
{
if (dest_len < base_len)
++abs_dir_cnt;
memcpy (ptr, dest_dir, dest_len);
ptr += dest_len;
readptr += base_len;
}
srcptr += len;
shrank += srcptr - readptr;
canonicalize_path ((char *)readptr, (char *)ptr);
if (win_path)
make_win_path((char *)ptr);
len = strlen ((char *)ptr) + 1;
shrank -= len;
ptr += len;
if (memcmp (orig, ptr - len, len))
dirty_section (DEBUG_STR);
free (orig);
}
if (shrank > 0)
{
if (--shrank == 0)
error (EXIT_FAILURE, 0,
"canonicalization unexpectedly shrank by one character");
else
{
memset (ptr, 'X', shrank);
ptr += shrank;
*ptr++ = '\0';
}
}
if (abs_dir_cnt + abs_file_cnt != 0)
{
size_t len = (abs_dir_cnt + abs_file_cnt) * (base_len - dest_len);
if (len == 1)
error (EXIT_FAILURE, 0, "-b arg has to be either the same length as -d arg, or more than 1 char longer");
memset (ptr, 'X', len - 1);
ptr += len - 1;
*ptr++ = '\0';
}
*ptr++ = '\0';
++srcptr;
while (*srcptr != 0)
{
size_t len = strlen ((char *)srcptr) + 1;
fprintf(debug_fd, "@@@@line srcptr %s\n", srcptr);
if (*srcptr == '/' && has_prefix ((char *)srcptr, base_dir))
{
memcpy (ptr, dest_dir, dest_len);
if (dest_len < base_len)
{
memmove (ptr + dest_len, srcptr + base_len,
len - base_len);
ptr += dest_len - base_len;
}
dirty_section (DEBUG_STR);
}
else if (ptr != srcptr)
memmove (ptr, srcptr, len);
srcptr += len;
ptr += len;
dir = srcptr;
read_uleb128 (srcptr);
read_uleb128 (srcptr);
read_uleb128 (srcptr);
if (ptr != dir)
memmove (ptr, dir, srcptr - dir);
ptr += srcptr - dir;
}
*ptr = '\0';
free (buf);
}
return 0;
}
static unsigned char *
edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
{
int i;
uint32_t list_offs;
int found_list_offs;
char *comp_dir;
comp_dir = NULL;
list_offs = 0;
found_list_offs = 0;
for (i = 0; i < t->nattr; ++i)
{
uint32_t form = t->attr[i].form;
size_t len = 0;
size_t base_len, dest_len;
while (1)
{
if (t->attr[i].attr == DW_AT_stmt_list)
{
if (form == DW_FORM_data4
|| form == DW_FORM_sec_offset)
{
list_offs = do_read_32_relocated (ptr);
found_list_offs = 1;
}
}
if (t->attr[i].attr == DW_AT_comp_dir)
{
if (form == DW_FORM_string)
{
free (comp_dir);
comp_dir = strdup ((char *)ptr);
fprintf(debug_fd, "####1comp_dir %s\n", comp_dir);
if (phase == 1 && dest_dir && has_prefix ((char *)ptr, base_dir))
{
base_len = strlen (base_dir);
dest_len = strlen (dest_dir);
fprintf(debug_fd, "####1updating base from %s to %s\n", base_dir, dest_dir);
memcpy (ptr, dest_dir, dest_len);
if (dest_len < base_len)
{
if (win_path)
memset(ptr + dest_len, '\\',
base_len - dest_len);
else
memset(ptr + dest_len, '/',
base_len - dest_len);
}
dirty_section (DEBUG_INFO);
}
}
else if (form == DW_FORM_strp &&
debug_sections[DEBUG_STR].data)
{
char *dir;
dir = (char *) debug_sections[DEBUG_STR].data
+ do_read_32_relocated (ptr);
free (comp_dir);
comp_dir = strdup (dir);
fprintf(debug_fd, "####2comp_dir %s\n", comp_dir);
if (phase == 1 && dest_dir && has_prefix (dir, base_dir))
{
base_len = strlen (base_dir);
dest_len = strlen (dest_dir);
fprintf(debug_fd, "####2updating base from %s to %s\n", base_dir, dest_dir);
memcpy (dir, dest_dir, dest_len);
if (dest_len < base_len)
{
memmove (dir + dest_len, dir + base_len,
strlen (dir + base_len) + 1);
}
dirty_section (DEBUG_STR);
}
}
}
else if ((t->tag == DW_TAG_compile_unit
|| t->tag == DW_TAG_partial_unit)
&& t->attr[i].attr == DW_AT_name)
{
char *name;
if (form == DW_FORM_strp && debug_sections[DEBUG_STR].data)
{
name = (char *) debug_sections[DEBUG_STR].data
+ do_read_32_relocated (ptr);
}
else if (form == DW_FORM_string && debug_sections[DEBUG_INFO].data)
{
name = (char *)(ptr);
}
fprintf(debug_fd, "====name %s\n", name);
/*
* If the compile unit has full path from root '/',
* and the compilation directory is still null,
* then we construct a compilation directory string.
*/
if (*name == '/' && comp_dir == NULL)
{
/*
* The strrchr() function shall locate the last
* occurrence of c (converted to a char) in the
* string pointed to by s. The terminating null
* byte is considered to be part of the string.
*
* Upon successful completion, strrchr() shall
* return a pointer to the byte or a null pointer
* if c does not occur in the string.
*/
char *enddir = strrchr (name, '/');
/*
* The compilation directory is constructed by
* removing the file name from the compile unit
* name attribute.
*/
if (enddir != name)
{
comp_dir = malloc (enddir - name + 1);
memcpy (comp_dir, name, enddir - name);
comp_dir [enddir - name] = '\0';
}
else
comp_dir = strdup ("/");
}
if (phase == 1 && dest_dir && has_prefix (name, base_dir))
{
base_len = strlen (base_dir);
dest_len = strlen (dest_dir);
fprintf(debug_fd, "====updating base from %s to %s\n", base_dir, dest_dir);
memcpy (name, dest_dir, dest_len);
if (form == DW_FORM_strp)
{
if (dest_len < base_len)