Merge from vendor branch BINUTILS:
[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 *
25 * $FreeBSD: src/libexec/rtld-elf/debug.c,v 1.2 1999/08/28 00:10:09 peter Exp $
d7dfcd52 26 * $DragonFly: src/libexec/rtld-elf/debug.c,v 1.4 2005/03/30 00:53:59 joerg Exp $
984263bc
MD
27 */
28
29/*
30 * Support for printing debugging messages.
31 */
32
33#include <stdarg.h>
34#include <stdio.h>
35
36#include "debug.h"
4a8d9350
JS
37#include "rtld.h"
38
39static const char rel_header[] =
40 " symbol name r_info r_offset st_value st_size address value\n"
41 " ------------------------------------------------------------------------------\n";
42static const char rel_format[] = " %-25s %6x %08x %08x %7d %10p %08x\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
53 fflush(stdout);
54 vfprintf(stderr, format, ap);
55 putc('\n', stderr);
56
57 va_end(ap);
58 }
59}
4a8d9350
JS
60
61void
62dump_relocations (Obj_Entry *obj0)
63{
64 Obj_Entry *obj;
65
66 for (obj = obj0; obj != NULL; obj = obj->next) {
67 dump_obj_relocations(obj);
68 }
69}
70
71void
72dump_obj_relocations (Obj_Entry *obj)
73{
74
75 printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
76
77 if (obj->relsize) {
78 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 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 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 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
102void
103dump_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 printf("%s", rel_header);
d7dfcd52 111 rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
4a8d9350
JS
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 printf(rel_format,
116 obj->strtab + sym->st_name,
117 rel->r_info, rel->r_offset,
118 sym->st_value, sym->st_size,
119 dstaddr, *dstaddr);
120 }
121 return;
122}
123
124void
125dump_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 printf("%s", rel_header);
d7dfcd52 133 relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
4a8d9350
JS
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 printf(rel_format,
138 obj->strtab + sym->st_name,
139 rela->r_info, rela->r_offset,
140 sym->st_value, sym->st_size,
141 dstaddr, *dstaddr);
142 }
143 return;
144}