xref: /freebsd/libexec/rtld-elf/debug.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*-
2  * Copyright 1996-1998 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 /*
29  * Support for printing debugging messages.
30  */
31 
32 #include <stdarg.h>
33 #include <stdio.h>
34 
35 #include "debug.h"
36 #include "rtld.h"
37 
38 static const char rel_header[] =
39     " symbol name               r_info r_offset st_value st_size    address    value\n"
40     " ------------------------------------------------------------------------------\n";
41 static const char rel_format[] =  " %-25s %6lx %08lx %08lx %7d %10p %08lx\n";
42 
43 int debug = 0;
44 
45 void
46 debug_printf(const char *format, ...)
47 {
48     if (debug) {
49 	va_list ap;
50 	va_start(ap, format);
51 
52 	fflush(stdout);
53 	vfprintf(stderr, format, ap);
54 	putc('\n', stderr);
55 
56 	va_end(ap);
57     }
58 }
59 
60 void
61 dump_relocations (Obj_Entry *obj0)
62 {
63     Obj_Entry *obj;
64 
65     for (obj = obj0; obj != NULL; obj = obj->next) {
66         dump_obj_relocations(obj);
67     }
68 }
69 
70 void
71 dump_obj_relocations (Obj_Entry *obj)
72 {
73 
74     printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
75 
76     if (obj->relsize) {
77         printf("Non-PLT Relocations: %ld\n",
78             (obj->relsize / sizeof(Elf_Rel)));
79         dump_Elf_Rel(obj, obj->rel, obj->relsize);
80     }
81 
82     if (obj->relasize) {
83         printf("Non-PLT Relocations with Addend: %ld\n",
84             (obj->relasize / sizeof(Elf_Rela)));
85         dump_Elf_Rela(obj, obj->rela, obj->relasize);
86     }
87 
88     if (obj->pltrelsize) {
89         printf("PLT Relocations: %ld\n",
90             (obj->pltrelsize / sizeof(Elf_Rel)));
91         dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
92     }
93 
94     if (obj->pltrelasize) {
95         printf("PLT Relocations with Addend: %ld\n",
96             (obj->pltrelasize / sizeof(Elf_Rela)));
97         dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
98     }
99 }
100 
101 void
102 dump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize)
103 {
104     const Elf_Rel *rel;
105     const Elf_Rel *rellim;
106     const Elf_Sym *sym;
107     Elf_Addr *dstaddr;
108 
109     printf("%s", rel_header);
110     rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
111     for (rel = rel0; rel < rellim; rel++) {
112 	dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset);
113         sym = obj->symtab + ELF_R_SYM(rel->r_info);
114         printf(rel_format,
115 		obj->strtab + sym->st_name,
116 		(u_long)rel->r_info, (u_long)rel->r_offset,
117 		(u_long)sym->st_value, (int)sym->st_size,
118 		dstaddr, (u_long)*dstaddr);
119     }
120     return;
121 }
122 
123 void
124 dump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize)
125 {
126     const Elf_Rela *rela;
127     const Elf_Rela *relalim;
128     const Elf_Sym *sym;
129     Elf_Addr *dstaddr;
130 
131     printf("%s", rel_header);
132     relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
133     for (rela = rela0; rela < relalim; rela++) {
134 	dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset);
135         sym = obj->symtab + ELF_R_SYM(rela->r_info);
136         printf(rel_format,
137 		obj->strtab + sym->st_name,
138 		(u_long)rela->r_info, (u_long)rela->r_offset,
139 		(u_long)sym->st_value, (int)sym->st_size,
140 		dstaddr, (u_long)*dstaddr);
141     }
142     return;
143 }
144