rtld: Implement ELF filters (DT_FILTER and DT_AUXILIARY)
[dragonfly.git] / libexec / rtld-elf / debug.c
CommitLineData
984263bc
MD
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 *
f4f4bfd5 25 * $FreeBSD$
984263bc
MD
26 */
27
28/*
29 * Support for printing debugging messages.
30 */
31
32#include <stdarg.h>
33#include <stdio.h>
34
35#include "debug.h"
4a8d9350 36#include "rtld.h"
abfcd5b1 37#include "rtld_printf.h"
4a8d9350
JS
38
39static const char rel_header[] =
40 " symbol name r_info r_offset st_value st_size address value\n"
41 " ------------------------------------------------------------------------------\n";
fcf53d9b 42static const char rel_format[] = " %-25s %6lx %08lx %08lx %7d %10p %08lx\n";
984263bc
MD
43
44int debug = 0;
45
46void
47debug_printf(const char *format, ...)
48{
49 if (debug) {
50 va_list ap;
51 va_start(ap, format);
52
abfcd5b1
JM
53 rtld_vfdprintf(STDERR_FILENO, format, ap);
54 rtld_fdputchar(STDERR_FILENO, '\n');
984263bc
MD
55
56 va_end(ap);
57 }
58}
4a8d9350
JS
59
60void
61dump_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
70void
71dump_obj_relocations (Obj_Entry *obj)
72{
73
abfcd5b1 74 rtld_printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
4a8d9350
JS
75
76 if (obj->relsize) {
abfcd5b1 77 rtld_printf("Non-PLT Relocations: %ld\n",
4a8d9350
JS
78 (obj->relsize / sizeof(Elf_Rel)));
79 dump_Elf_Rel(obj, obj->rel, obj->relsize);
80 }
81
82 if (obj->relasize) {
abfcd5b1 83 rtld_printf("Non-PLT Relocations with Addend: %ld\n",
4a8d9350
JS
84 (obj->relasize / sizeof(Elf_Rela)));
85 dump_Elf_Rela(obj, obj->rela, obj->relasize);
86 }
87
88 if (obj->pltrelsize) {
abfcd5b1 89 rtld_printf("PLT Relocations: %ld\n",
4a8d9350
JS
90 (obj->pltrelsize / sizeof(Elf_Rel)));
91 dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
92 }
93
94 if (obj->pltrelasize) {
abfcd5b1 95 rtld_printf("PLT Relocations with Addend: %ld\n",
4a8d9350
JS
96 (obj->pltrelasize / sizeof(Elf_Rela)));
97 dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
98 }
99}
100
101void
102dump_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
abfcd5b1 109 rtld_putstr(rel_header);
d7dfcd52 110 rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
4a8d9350
JS
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);
abfcd5b1 114 rtld_printf(rel_format,
4a8d9350 115 obj->strtab + sym->st_name,
fcf53d9b
JM
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);
4a8d9350
JS
119 }
120 return;
121}
122
123void
124dump_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
abfcd5b1 131 rtld_putstr(rel_header);
d7dfcd52 132 relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
4a8d9350
JS
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);
abfcd5b1 136 rtld_printf(rel_format,
4a8d9350 137 obj->strtab + sym->st_name,
fcf53d9b
JM
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);
4a8d9350
JS
141 }
142 return;
143}