Debian Codegraph @

src/bin/dwarfexample/dwdebuglink.c

raw
1 /* David Anderson. 2019-2020. This small program is hereby
2 placed into the public domain to be copied or
3 used by anyone for any purpose.
4
5 See
6 https://sourceware.org/gdb/onlinedocs/\
7 gdb/Separate-Debug-Files.html
8
9 An emerging GNU pattern seems to be:
10 If a path found from debuglink or
11 build-id matches an elf object then
12 both the objects may bave a build-id
13 and if it is the right object the build-ids
14 will match. This pattern does not
15 refer to the crc from the executable
16 debuglink.
17 To build a separate debug file x.debug with
18 DWARF and an executable with just debugid
19 and debuglink data using the pattern
20 seen in Ubuntu 20.04:
21
22 first compile and link, creating x
23 then:
24 objcopy --only-keep-debug x x.debug
25 objcopy --strip-debug x
26 objcopy --add-gnu-debuglink=x.debug x
27
28 See 'man objcopy' or
29 https://sourceware.org/binutils/docs/binutils/objcopy.html
30 for more information.
31 */
32
33 #include <config.h>
34
35 #include <stdio.h> /* printf() snprintf() */
36 #include <stdlib.h> /* exit() free() malloc() */
37 #include <string.h> /* memset() strdup() strlen() strncmp() */
38
39 #include "dwarf.h"
40 #include "libdwarf.h"
41 #include "libdwarf_private.h"
42
43 char trueoutpath[2000];
44 static const char *dlname = ".gnu_debuglink";
45 static const char *buildidname = ".note.gnu.buildid";
46
47 static int doprintbuildid = 1;
48 static int doprintdebuglink = 1;
49
50 static void
51 dump_bytes(const char *prefix,
52 char *msg,
53 unsigned char *start,
54 long len)
55 {
56 Dwarf_Small *end = start + len;
57 Dwarf_Small *cur = start;
58 if (!start) {
59 printf("%s ptr null, ignore. \n",msg);
60 return;
61 }
62 printf("%s%s ",prefix,msg);
63 for (; cur < end; cur++) {
64 printf("%02x ", *cur);
65 }
66 printf("\n");
67 }
68
69 static int
70 blockmatch(unsigned char *l,
71 unsigned char* r,
72 unsigned length)
73 {
74 unsigned int i = 0;
75 for ( ; i < length; ++i) {
76 if (l[i] != r[i]) {
77 return FALSE;
78 }
79 }
80 return TRUE;
81 }
82
83 static void
84 print_debuglink(const char *prefix,
85 char *debuglinkpath,
86 unsigned char *crc,
87 char *debuglinkfullpath,
88 unsigned int debuglinkfullpath_strlen)
89 {
90 unsigned char *crcx = 0;
91 unsigned char *end = 0;
92
93 printf("%s Section : %s\n",prefix,dlname);
94 printf("%s Debuglink name : %s\n",prefix,debuglinkpath);
95 crcx = crc;
96 end = crcx + 4;
97 printf("%s compiler-created crc: 0X ",prefix);
98 for (; crcx < end; crcx++) {
99 printf("%02x ", *crcx);
100 }
101 printf("\n");
102 if (debuglinkfullpath_strlen) {
103 printf("%s Debuglink target : %s\n",
104 prefix,debuglinkfullpath);
105 }
106 }
107
108 static void
109 print_buildid(const char *prefix,
110 unsigned int buildid_type,
111 char *buildidownername,
112 unsigned int buildid_length,
113 unsigned char *buildid)
114 {
115 printf("%s Section : %s\n",prefix,buildidname);
116 printf("%s Build-id type : %u\n",prefix, buildid_type);
117 printf("%s Build-id ownername : %s\n",prefix,
118 buildidownername);
119 printf("%s Build-id length : %u\n",prefix,buildid_length);
120 printf("%s Build-id : ",prefix);
121 {
122 const unsigned char *cur = 0;
123 const unsigned char *end = 0;
124
125 cur = buildid;
126 end = cur + buildid_length;
127 for (; cur < end; cur++) {
128 printf("%02x", (unsigned char)*cur);
129 }
130 }
131 printf("\n");
132 }
133
134 /* Returns TRUE if its a real file of some interest. */
135 static int
136 print_ftype_message(const char * prefix,
137 unsigned int ftype)
138 {
139 switch(ftype) {
140 case DW_FTYPE_ELF:
141 printf("%s file above is an Elf object\n",prefix);
142 return TRUE;
143 case DW_FTYPE_MACH_O:
144 printf("%s file above is a Mach-O object\n",prefix);
145 return TRUE;
146 case DW_FTYPE_PE:
147 printf("%s file above is a PE object",prefix);
148 return TRUE;
149 case DW_FTYPE_ARCHIVE:
150 printf("%s file above is an archive so ignore it.",
151 prefix);
152 return FALSE;
153 default:
154 printf("%s file above is not an object type"
155 " we recognize\n",prefix);
156 }
157 return FALSE;
158 }
159
160 /* The debug version we expect not to have debuglink,
161 checking here if buildid crc and id length match.
162 Never returns DW_DLV_ERROR.
163 debuglink records have a crc.
164 build ID records have a byte string
165 This matches either.
166 */
167 static int
168 match_buildid(const char *prefix,
169 unsigned char *crc_base,
170 unsigned buildid_length_base,
171 unsigned char * buildid_base,
172 /* *_base is executable info while
173 *_debug is the debug object. */
174 unsigned char * crc_debug,
175 unsigned buildid_length_debug,
176 unsigned char *buildid_debug)
177 {
178 if (crc_debug && crc_base) {
179 /* crc available for both */
180 if (!blockmatch(crc_debug,crc_base,4)) {
181 dump_bytes(prefix," crc base ",crc_base,4);
182 dump_bytes(prefix," crc target",crc_debug,4);
183 printf("%s===crc does not match\n",prefix);
184 return DW_DLV_NO_ENTRY;
185 }
186 /* Matching crc, applicable to debuglink */
187 return DW_DLV_OK;
188 } else {
189 }
190 if (buildid_length_base != buildid_length_debug) {
191 printf("%s===buildid length does not match",prefix);
192 return DW_DLV_NO_ENTRY;
193 }
194 if (!blockmatch(buildid_base,buildid_debug,
195 buildid_length_base)) {
196 printf("%s===buildid does not match\n",prefix);
197 return DW_DLV_NO_ENTRY;
198 }
199 return DW_DLV_OK;
200 }
201
202 #ifdef WORDS_BIGENDIAN
203 /* Trivial hack to adjust for debuglink,
204 so we can have littleendian baseline match
205 bigendian make check. */
206 static void
207 swapbytes(unsigned char x[4], int len)
208 {
209 unsigned char y[4];
210 int i;
211
212 if (len != 4) {
213 return;
214 }
215 for (i = 0; i < len;++i) {
216 y[i] = x[len-1 -i];
217 }
218 memcpy(x,y,len);
219 }
220 #endif /* WORDS_BIGENDIAN */
221
222 static int
223 one_file_debuglink_internal(int is_outer,const char *prefix,
224 char **gl_pathnames,
225 unsigned gl_pathcount,
226 int no_follow_debuglink,
227 char *path_in,
228 unsigned char *crc_in,
229 unsigned buildid_len_in,
230 unsigned char *buildid_in,
231 char *debug_path_in)
232 {
233 int res = 0;
234 Dwarf_Debug dbg = 0;
235 unsigned i = 0;
236 char *debuglinkpath = 0; /* must be freed */
237 unsigned char *crc = 0;
238 char *debuglinkfullpath = 0;
239 unsigned debuglinkfullpath_strlen = 0;
240 unsigned buildid_type = 0;
241 char * buildidownername = 0;
242 unsigned char *buildid = 0;
243 unsigned buildid_length = 0;
244 char ** paths = 0; /* must be freed */
245 unsigned paths_count = 0;
246 char * path = 0;
247 char * basepath = 0;
248 Dwarf_Error error = 0;
249 unsigned int p = 0;
250
251 /* Don't let dwarf_init_path find the debuglink,
252 we want to do it here so we can show it all. */
253 path = basepath = path_in;
254 if (!is_outer) {
255 path = debug_path_in;
256 printf("%s===Referred-path : %s\n",prefix,debug_path_in);
257 } else {
258 printf("%s===Exec-path : %s\n",prefix,basepath);
259 }
260 res = dwarf_init_path(path,
261 0,0,
262 DW_GROUPNUMBER_ANY,
263 0,0, &dbg, &error);
264 if (res == DW_DLV_ERROR) {
265 printf("%sError from libdwarf opening \"%s\": %s\n",
266 prefix, path, dwarf_errmsg(error));
267 dwarf_dealloc_error(dbg,error);
268 error = 0;
269 return res;
270 }
271 if (res == DW_DLV_NO_ENTRY) {
272 printf("%sThere is no such file as \"%s\"\n",
273 prefix, path);
274 return DW_DLV_NO_ENTRY;
275 }
276 if (is_outer && no_follow_debuglink) {
277 printf("%s no follow debuglink: TRUE\n",prefix);
278 }
279 for (p = 0; p < gl_pathcount; ++p) {
280 const char *lpath = 0;
281
282 lpath = (const char *)gl_pathnames[p];
283 res = dwarf_add_debuglink_global_path(dbg,
284 lpath, &error);
285 if (res != DW_DLV_OK){
286 printf("Failed add global path. result %d line %d\n",
287 res,__LINE__);
288 exit(EXIT_FAILURE);
289 }
290 printf("%s global path : %s\n",prefix,lpath);
291 }
292 res = dwarf_gnu_debuglink(dbg,
293 &debuglinkpath,
294 &crc, &debuglinkfullpath, &debuglinkfullpath_strlen,
295 &buildid_type, &buildidownername,
296 &buildid, &buildid_length,
297 &paths, &paths_count, &error);
298 if (res == DW_DLV_ERROR) {
299 printf("%sError from libdwarf accessing debuglink "
300 "related sections in \"%s\": %s\n",
301 prefix, path, dwarf_errmsg(error));
302 dwarf_dealloc_error(dbg,error);
303 error = 0;
304 dwarf_finish(dbg);
305 return res;
306 }
307 if (res == DW_DLV_NO_ENTRY) {
308 printf("%sThere is no %s or %s section in \"%s\"\n",
309 prefix,dlname,buildidname,path);
310 dwarf_finish(dbg);
311 return DW_DLV_NO_ENTRY;
312 }
313 if (doprintdebuglink && crc) {
314 print_debuglink(prefix,debuglinkpath,crc,
315 debuglinkfullpath,debuglinkfullpath_strlen);
316 }
317 if (doprintbuildid && buildid) {
318 print_buildid(prefix, buildid_type,
319 buildidownername, buildid_length, buildid);
320 }
321 if (!is_outer) {
322 unsigned char lcrc[4];
323
324 /* dbg might be the correct .debug object */
325 memset(&lcrc[0],0,sizeof(lcrc));
326 if (crc_in && !crc) {
327 res = dwarf_crc32(dbg,lcrc,&error);
328 if (res == DW_DLV_ERROR) {
329 dwarf_dealloc_error(dbg,error);
330 error = 0;
331 } else if (res == DW_DLV_OK) {
332 /* If the system is big endian
333 swap the bytes in lcrc
334 as the test baseline is little-endian. */
335 #ifdef WORDS_BIGENDIAN
336 swapbytes(lcrc,sizeof(lcrc));
337 #endif /* WORDS_BIGENDIAN */
338 crc = &lcrc[0];
339 }
340 }
341 res = match_buildid(prefix,
342 /* This is the executable */
343 crc_in,buildid_len_in,buildid_in,
344 crc,buildid_length,buildid);
345 if (res == DW_DLV_OK) {
346 printf("%s===executable and debug buildid match\n",
347 prefix);
348 }
349 dwarf_finish(dbg);
350 free(paths);
351 return DW_DLV_NO_ENTRY;
352 }
353 /* If debug_path_in then this list does not
354 mean anything. */
355
356 for (i =0; is_outer && i < paths_count; ++i) {
357 char *pa = paths[i];
358 unsigned int ftype = 0;
359 unsigned int endian = 0;
360 unsigned int offsetsize = 0;
361 Dwarf_Unsigned filesize = 0;
362 int errcode = 0;
363 int realobj = TRUE;
364 static char lprefix[50];
365
366 snprintf(lprefix,sizeof(lprefix), " [%2u]",i);
367 printf("%s Path [%2u] %s\n",lprefix,i,pa);
368 /* First, open the file to determine if it exists.
369 If not, loop again */
370 res = dwarf_object_detector_path_b(pa,
371 0,0,0,0,
372 &ftype,&endian,&offsetsize,
373 &filesize, 0, &errcode);
374 if (res == DW_DLV_NO_ENTRY) {
375 printf("%s file above does not match/exist.\n",lprefix);
376 continue;
377 }
378 if (res == DW_DLV_ERROR) {
379 printf("%s file above access attempt "
380 "lead to error %s\n",
381 dwarf_errmsg_by_number(errcode),lprefix);
382 continue;
383 }
384 realobj = print_ftype_message(lprefix, ftype);
385 if (!realobj) {
386 continue;
387 }
388 if (is_outer && !no_follow_debuglink) {
389 /* read the executable and look to the
390 debug (ie pa) to see if it matches.
391 Do not pass in globals paths*/
392 res = one_file_debuglink_internal(
393 FALSE,lprefix,0,0,0,
394 pa,crc,buildid_length, buildid,pa);
395 if (res == DW_DLV_OK) {
396 printf("%s =====File %s is a correct"
397 " .debug object\n\n", lprefix,pa);
398 }
399 }
400 }
401 free(paths);
402 free(debuglinkfullpath);
403 dwarf_finish(dbg);
404 return DW_DLV_OK;
405 }
406
407 static void
408 one_file_debuglink(char *path,char **dlpaths,unsigned int dlcount,
409 int no_follow_debuglink)
410 {
411 one_file_debuglink_internal(TRUE,"",dlpaths,dlcount,
412 no_follow_debuglink,
413 path,0,0,0,0);
414 }
415
416 static char **gl_pathnames;
417 static unsigned int gl_pathcount;
418 static void add_a_path(char *path)
419 {
420 unsigned count;
421 char ** newpathnames = 0;
422 unsigned int newslen = 0;
423 unsigned int i = 0;
424
425 if (!path) {
426 printf("Null debuglink path error\n");
427 exit(EXIT_FAILURE);
428 }
429 newslen = strlen(path);
430 if (!newslen){
431 printf("Empty debuglink path ignored\n");
432 return;
433 }
434 count = gl_pathcount + 1;
435 newpathnames = (char **)malloc(sizeof(char *) *count);
436 if (!newpathnames) {
437 printf("Out of malloc space? giving up.\n");
438 exit(EXIT_FAILURE);
439 }
440 for (i = 0; i < gl_pathcount; ++i) {
441 newpathnames[i] = gl_pathnames[i];
442 }
443 newpathnames[i] = strdup(path);
444 if (!newpathnames[i]) {
445 printf("Out of malloc space? giving up.\n");
446 exit(EXIT_FAILURE);
447 }
448 free(gl_pathnames);
449 gl_pathcount = count;
450 gl_pathnames = newpathnames;
451 }
452 static void free_paths(void)
453 {
454 unsigned i = 0;
455 if (!gl_pathcount) {
456 return;
457 }
458 for (i = 0; i < gl_pathcount; ++i) {
459 free(gl_pathnames[i]);
460 gl_pathnames[i] = 0;
461 }
462 free(gl_pathnames);
463 gl_pathnames = 0;
464 }
465
466 static char *
467 basename(char *filepath)
468 {
469 unsigned long lastindex = 0;
470 char *cp = filepath;
471 unsigned long count = 0;
472 char *basename = 0;
473 int found = FALSE;
474 int c = 0;
475
476 for ( ; *cp ; ++cp,++count) {
477 c = *cp;
478 if (c == '/') {
479 lastindex = count;
480 found = TRUE;
481 }
482 }
483 if (!found) {
484 /* No directory in the name */
485 return filepath;
486 }
487 basename = filepath +lastindex+1;
488 c = *basename;
489 if (!c) {
490 /* Something is very wrong here */
491 return filepath;
492 }
493 return basename;
494 }
495
496 int
497 main(int argc, char **argv)
498 {
499 int i = 1;
500 char *filenamein = 0;
501 int no_follow_debuglink = FALSE;
502
503 for ( ; i < argc; ++i) {
504 char *arg = argv[i];
505 if (!strncmp(arg,"--no-follow-debuglink",21)) {
506 no_follow_debuglink = TRUE;
507 continue;
508 }
509 if (!strncmp(arg,"--add-debuglink-path=",21)){
510 add_a_path(arg+21);
511 continue;
512 }
513 if (!strcmp(arg,"--suppress-de-alloc-tree")){
514 /* do nothing, ignore the argument */
515 continue;
516 }
517 filenamein = arg;
518 one_file_debuglink(filenamein,gl_pathnames,gl_pathcount,
519 no_follow_debuglink);
520 printf("=======done with %s\n",basename(filenamein));
521 }
522 free_paths();
523 return 0;
524 }