1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
28 */
29
30 /*
31 * Support for printing debugging messages.
32 */
33
34 #include <stdarg.h>
35 #include <stdio.h>
36
37 #include "debug.h"
38 #include "rtld.h"
39 #include "rtld_printf.h"
40
41 static const char rel_header[] =
42 " symbol name r_info r_offset st_value st_size address value\n"
43 " ------------------------------------------------------------------------------\n";
44 static const char rel_format[] = " %-25s %6lx %08lx %08lx %7d %10p %08lx\n";
45
46 int debug = 0;
47
48 void
debug_printf(const char * format,...)49 debug_printf(const char *format, ...)
50 {
51 if (debug) {
52 va_list ap;
53 va_start(ap, format);
54
55 rtld_vfdprintf(STDERR_FILENO, format, ap);
56 rtld_fdputchar(STDERR_FILENO, '\n');
57
58 va_end(ap);
59 }
60 }
61
62 void
dump_relocations(Obj_Entry * obj0)63 dump_relocations (Obj_Entry *obj0)
64 {
65 Obj_Entry *obj;
66
67 for (obj = globallist_curr(obj0); obj != NULL;
68 obj = globallist_next(obj)) {
69 dump_obj_relocations(obj);
70 }
71 }
72
73 void
dump_obj_relocations(Obj_Entry * obj)74 dump_obj_relocations (Obj_Entry *obj)
75 {
76
77 rtld_printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
78
79 if (obj->relsize) {
80 rtld_printf("Non-PLT Relocations: %ld\n",
81 (obj->relsize / sizeof(Elf_Rel)));
82 dump_Elf_Rel(obj, obj->rel, obj->relsize);
83 }
84
85 if (obj->relasize) {
86 rtld_printf("Non-PLT Relocations with Addend: %ld\n",
87 (obj->relasize / sizeof(Elf_Rela)));
88 dump_Elf_Rela(obj, obj->rela, obj->relasize);
89 }
90
91 if (obj->pltrelsize) {
92 rtld_printf("PLT Relocations: %ld\n",
93 (obj->pltrelsize / sizeof(Elf_Rel)));
94 dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
95 }
96
97 if (obj->pltrelasize) {
98 rtld_printf("PLT Relocations with Addend: %ld\n",
99 (obj->pltrelasize / sizeof(Elf_Rela)));
100 dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
101 }
102 }
103
104 void
dump_Elf_Rel(Obj_Entry * obj,const Elf_Rel * rel0,u_long relsize)105 dump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize)
106 {
107 const Elf_Rel *rel;
108 const Elf_Rel *rellim;
109 const Elf_Sym *sym;
110 Elf_Addr *dstaddr;
111
112 rtld_putstr(rel_header);
113 rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
114 for (rel = rel0; rel < rellim; rel++) {
115 dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset);
116 sym = obj->symtab + ELF_R_SYM(rel->r_info);
117 rtld_printf(rel_format,
118 obj->strtab + sym->st_name,
119 (u_long)rel->r_info, (u_long)rel->r_offset,
120 (u_long)sym->st_value, (int)sym->st_size,
121 dstaddr, (u_long)*dstaddr);
122 }
123 return;
124 }
125
126 void
dump_Elf_Rela(Obj_Entry * obj,const Elf_Rela * rela0,u_long relasize)127 dump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize)
128 {
129 const Elf_Rela *rela;
130 const Elf_Rela *relalim;
131 const Elf_Sym *sym;
132 Elf_Addr *dstaddr;
133
134 rtld_putstr(rel_header);
135 relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
136 for (rela = rela0; rela < relalim; rela++) {
137 dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset);
138 sym = obj->symtab + ELF_R_SYM(rela->r_info);
139 rtld_printf(rel_format,
140 obj->strtab + sym->st_name,
141 (u_long)rela->r_info, (u_long)rela->r_offset,
142 (u_long)sym->st_value, (int)sym->st_size,
143 dstaddr, (u_long)*dstaddr);
144 }
145 return;
146 }
147