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