xref: /freebsd-13.1/libexec/rtld-elf/debug.c (revision c5d061c1)
13124c3e0SJohn Polstra /*-
23124c3e0SJohn Polstra  * Copyright 1996-1998 John D. Polstra.
33124c3e0SJohn Polstra  * All rights reserved.
43124c3e0SJohn Polstra  *
53124c3e0SJohn Polstra  * Redistribution and use in source and binary forms, with or without
63124c3e0SJohn Polstra  * modification, are permitted provided that the following conditions
73124c3e0SJohn Polstra  * are met:
83124c3e0SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
93124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
103124c3e0SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
113124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
123124c3e0SJohn Polstra  *    documentation and/or other materials provided with the distribution.
133124c3e0SJohn Polstra  *
143124c3e0SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153124c3e0SJohn Polstra  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163124c3e0SJohn Polstra  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173124c3e0SJohn Polstra  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183124c3e0SJohn Polstra  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193124c3e0SJohn Polstra  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203124c3e0SJohn Polstra  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213124c3e0SJohn Polstra  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223124c3e0SJohn Polstra  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233124c3e0SJohn Polstra  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243124c3e0SJohn Polstra  *
257f3dea24SPeter Wemm  * $FreeBSD$
263124c3e0SJohn Polstra  */
273124c3e0SJohn Polstra 
283124c3e0SJohn Polstra /*
293124c3e0SJohn Polstra  * Support for printing debugging messages.
303124c3e0SJohn Polstra  */
313124c3e0SJohn Polstra 
323124c3e0SJohn Polstra #include <stdarg.h>
333124c3e0SJohn Polstra #include <stdio.h>
343124c3e0SJohn Polstra 
353124c3e0SJohn Polstra #include "debug.h"
36*c5d061c1SMatthew N. Dodd #include "rtld.h"
37*c5d061c1SMatthew N. Dodd 
38*c5d061c1SMatthew N. Dodd static const char rel_header[] =
39*c5d061c1SMatthew N. Dodd     " symbol name               r_info r_offset st_value st_size    address    value\n"
40*c5d061c1SMatthew N. Dodd     " ------------------------------------------------------------------------------\n";
41*c5d061c1SMatthew N. Dodd static const char rel_format[] =  " %-25s %6x %08x %08x %7d %10p %08x\n";
423124c3e0SJohn Polstra 
433124c3e0SJohn Polstra int debug = 0;
443124c3e0SJohn Polstra 
453124c3e0SJohn Polstra void
463124c3e0SJohn Polstra debug_printf(const char *format, ...)
473124c3e0SJohn Polstra {
483124c3e0SJohn Polstra     if (debug) {
493124c3e0SJohn Polstra 	va_list ap;
503124c3e0SJohn Polstra 	va_start(ap, format);
513124c3e0SJohn Polstra 
523124c3e0SJohn Polstra 	fflush(stdout);
533124c3e0SJohn Polstra 	vfprintf(stderr, format, ap);
543124c3e0SJohn Polstra 	putc('\n', stderr);
553124c3e0SJohn Polstra 
563124c3e0SJohn Polstra 	va_end(ap);
573124c3e0SJohn Polstra     }
583124c3e0SJohn Polstra }
59*c5d061c1SMatthew N. Dodd 
60*c5d061c1SMatthew N. Dodd void
61*c5d061c1SMatthew N. Dodd dump_relocations (Obj_Entry *obj0)
62*c5d061c1SMatthew N. Dodd {
63*c5d061c1SMatthew N. Dodd     Obj_Entry *obj;
64*c5d061c1SMatthew N. Dodd 
65*c5d061c1SMatthew N. Dodd     for (obj = obj0; obj != NULL; obj = obj->next) {
66*c5d061c1SMatthew N. Dodd         dump_obj_relocations(obj);
67*c5d061c1SMatthew N. Dodd     }
68*c5d061c1SMatthew N. Dodd }
69*c5d061c1SMatthew N. Dodd 
70*c5d061c1SMatthew N. Dodd void
71*c5d061c1SMatthew N. Dodd dump_obj_relocations (Obj_Entry *obj)
72*c5d061c1SMatthew N. Dodd {
73*c5d061c1SMatthew N. Dodd 
74*c5d061c1SMatthew N. Dodd     printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
75*c5d061c1SMatthew N. Dodd 
76*c5d061c1SMatthew N. Dodd     if (obj->relsize) {
77*c5d061c1SMatthew N. Dodd         printf("Non-PLT Relocations: %ld\n",
78*c5d061c1SMatthew N. Dodd             (obj->relsize / sizeof(Elf_Rel)));
79*c5d061c1SMatthew N. Dodd         dump_Elf_Rel(obj, obj->rel, obj->relsize);
80*c5d061c1SMatthew N. Dodd     }
81*c5d061c1SMatthew N. Dodd 
82*c5d061c1SMatthew N. Dodd     if (obj->relasize) {
83*c5d061c1SMatthew N. Dodd         printf("Non-PLT Relocations with Addend: %ld\n",
84*c5d061c1SMatthew N. Dodd             (obj->relasize / sizeof(Elf_Rela)));
85*c5d061c1SMatthew N. Dodd         dump_Elf_Rela(obj, obj->rela, obj->relasize);
86*c5d061c1SMatthew N. Dodd     }
87*c5d061c1SMatthew N. Dodd 
88*c5d061c1SMatthew N. Dodd     if (obj->pltrelsize) {
89*c5d061c1SMatthew N. Dodd         printf("PLT Relocations: %ld\n",
90*c5d061c1SMatthew N. Dodd             (obj->pltrelsize / sizeof(Elf_Rel)));
91*c5d061c1SMatthew N. Dodd         dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
92*c5d061c1SMatthew N. Dodd     }
93*c5d061c1SMatthew N. Dodd 
94*c5d061c1SMatthew N. Dodd     if (obj->pltrelasize) {
95*c5d061c1SMatthew N. Dodd         printf("PLT Relocations with Addend: %ld\n",
96*c5d061c1SMatthew N. Dodd             (obj->pltrelasize / sizeof(Elf_Rela)));
97*c5d061c1SMatthew N. Dodd         dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
98*c5d061c1SMatthew N. Dodd     }
99*c5d061c1SMatthew N. Dodd }
100*c5d061c1SMatthew N. Dodd 
101*c5d061c1SMatthew N. Dodd void
102*c5d061c1SMatthew N. Dodd dump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize)
103*c5d061c1SMatthew N. Dodd {
104*c5d061c1SMatthew N. Dodd     const Elf_Rel *rel;
105*c5d061c1SMatthew N. Dodd     const Elf_Rel *rellim;
106*c5d061c1SMatthew N. Dodd     const Elf_Sym *sym;
107*c5d061c1SMatthew N. Dodd     Elf_Addr *dstaddr;
108*c5d061c1SMatthew N. Dodd 
109*c5d061c1SMatthew N. Dodd     printf("%s", rel_header);
110*c5d061c1SMatthew N. Dodd     rellim = (const Elf_Rel *)((char *)rel0 + relsize);
111*c5d061c1SMatthew N. Dodd     for (rel = rel0; rel < rellim; rel++) {
112*c5d061c1SMatthew N. Dodd 	dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset);
113*c5d061c1SMatthew N. Dodd         sym = obj->symtab + ELF_R_SYM(rel->r_info);
114*c5d061c1SMatthew N. Dodd         printf(rel_format,
115*c5d061c1SMatthew N. Dodd 		obj->strtab + sym->st_name,
116*c5d061c1SMatthew N. Dodd 		rel->r_info, rel->r_offset,
117*c5d061c1SMatthew N. Dodd 		sym->st_value, sym->st_size,
118*c5d061c1SMatthew N. Dodd 		dstaddr, *dstaddr);
119*c5d061c1SMatthew N. Dodd     }
120*c5d061c1SMatthew N. Dodd     return;
121*c5d061c1SMatthew N. Dodd }
122*c5d061c1SMatthew N. Dodd 
123*c5d061c1SMatthew N. Dodd void
124*c5d061c1SMatthew N. Dodd dump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize)
125*c5d061c1SMatthew N. Dodd {
126*c5d061c1SMatthew N. Dodd     const Elf_Rela *rela;
127*c5d061c1SMatthew N. Dodd     const Elf_Rela *relalim;
128*c5d061c1SMatthew N. Dodd     const Elf_Sym *sym;
129*c5d061c1SMatthew N. Dodd     Elf_Addr *dstaddr;
130*c5d061c1SMatthew N. Dodd 
131*c5d061c1SMatthew N. Dodd     printf("%s", rel_header);
132*c5d061c1SMatthew N. Dodd     relalim = (const Elf_Rela *)((char *)rela0 + relasize);
133*c5d061c1SMatthew N. Dodd     for (rela = rela0; rela < relalim; rela++) {
134*c5d061c1SMatthew N. Dodd 	dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset);
135*c5d061c1SMatthew N. Dodd         sym = obj->symtab + ELF_R_SYM(rela->r_info);
136*c5d061c1SMatthew N. Dodd         printf(rel_format,
137*c5d061c1SMatthew N. Dodd 		obj->strtab + sym->st_name,
138*c5d061c1SMatthew N. Dodd 		rela->r_info, rela->r_offset,
139*c5d061c1SMatthew N. Dodd 		sym->st_value, sym->st_size,
140*c5d061c1SMatthew N. Dodd 		dstaddr, *dstaddr);
141*c5d061c1SMatthew N. Dodd     }
142*c5d061c1SMatthew N. Dodd     return;
143*c5d061c1SMatthew N. Dodd }
144