debug.c revision 116582
134192Sjdp/*-
234192Sjdp * Copyright 1996-1998 John D. Polstra.
334192Sjdp * All rights reserved.
434192Sjdp *
534192Sjdp * Redistribution and use in source and binary forms, with or without
634192Sjdp * modification, are permitted provided that the following conditions
734192Sjdp * are met:
834192Sjdp * 1. Redistributions of source code must retain the above copyright
934192Sjdp *    notice, this list of conditions and the following disclaimer.
1034192Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1134192Sjdp *    notice, this list of conditions and the following disclaimer in the
1234192Sjdp *    documentation and/or other materials provided with the distribution.
1334192Sjdp *
1434192Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1534192Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1634192Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1734192Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1834192Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1934192Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2034192Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2134192Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2234192Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2334192Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2434192Sjdp *
2550476Speter * $FreeBSD: head/libexec/rtld-elf/debug.c 116582 2003-06-19 16:09:18Z mdodd $
2634192Sjdp */
2734192Sjdp
2834192Sjdp/*
2934192Sjdp * Support for printing debugging messages.
3034192Sjdp */
3134192Sjdp
3234192Sjdp#include <stdarg.h>
3334192Sjdp#include <stdio.h>
3434192Sjdp
3534192Sjdp#include "debug.h"
36116563Smdodd#include "rtld.h"
3734192Sjdp
38116563Smdoddstatic const char rel_header[] =
39116563Smdodd    " symbol name               r_info r_offset st_value st_size    address    value\n"
40116563Smdodd    " ------------------------------------------------------------------------------\n";
41116582Smdoddstatic const char rel_format[] =  " %-25s %6lx %08lx %08lx %7d %10p %08lx\n";
42116563Smdodd
4334192Sjdpint debug = 0;
4434192Sjdp
4534192Sjdpvoid
4634192Sjdpdebug_printf(const char *format, ...)
4734192Sjdp{
4834192Sjdp    if (debug) {
4934192Sjdp	va_list ap;
5034192Sjdp	va_start(ap, format);
5134192Sjdp
5234192Sjdp	fflush(stdout);
5334192Sjdp	vfprintf(stderr, format, ap);
5434192Sjdp	putc('\n', stderr);
5534192Sjdp
5634192Sjdp	va_end(ap);
5734192Sjdp    }
5834192Sjdp}
59116563Smdodd
60116563Smdoddvoid
61116563Smdodddump_relocations (Obj_Entry *obj0)
62116563Smdodd{
63116563Smdodd    Obj_Entry *obj;
64116563Smdodd
65116563Smdodd    for (obj = obj0; obj != NULL; obj = obj->next) {
66116563Smdodd        dump_obj_relocations(obj);
67116563Smdodd    }
68116563Smdodd}
69116563Smdodd
70116563Smdoddvoid
71116563Smdodddump_obj_relocations (Obj_Entry *obj)
72116563Smdodd{
73116563Smdodd
74116563Smdodd    printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
75116563Smdodd
76116563Smdodd    if (obj->relsize) {
77116563Smdodd        printf("Non-PLT Relocations: %ld\n",
78116563Smdodd            (obj->relsize / sizeof(Elf_Rel)));
79116563Smdodd        dump_Elf_Rel(obj, obj->rel, obj->relsize);
80116563Smdodd    }
81116563Smdodd
82116563Smdodd    if (obj->relasize) {
83116563Smdodd        printf("Non-PLT Relocations with Addend: %ld\n",
84116563Smdodd            (obj->relasize / sizeof(Elf_Rela)));
85116563Smdodd        dump_Elf_Rela(obj, obj->rela, obj->relasize);
86116563Smdodd    }
87116563Smdodd
88116563Smdodd    if (obj->pltrelsize) {
89116563Smdodd        printf("PLT Relocations: %ld\n",
90116563Smdodd            (obj->pltrelsize / sizeof(Elf_Rel)));
91116563Smdodd        dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
92116563Smdodd    }
93116563Smdodd
94116563Smdodd    if (obj->pltrelasize) {
95116563Smdodd        printf("PLT Relocations with Addend: %ld\n",
96116563Smdodd            (obj->pltrelasize / sizeof(Elf_Rela)));
97116563Smdodd        dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
98116563Smdodd    }
99116563Smdodd}
100116563Smdodd
101116563Smdoddvoid
102116563Smdodddump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize)
103116563Smdodd{
104116563Smdodd    const Elf_Rel *rel;
105116563Smdodd    const Elf_Rel *rellim;
106116563Smdodd    const Elf_Sym *sym;
107116563Smdodd    Elf_Addr *dstaddr;
108116563Smdodd
109116563Smdodd    printf("%s", rel_header);
110116582Smdodd    rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
111116563Smdodd    for (rel = rel0; rel < rellim; rel++) {
112116563Smdodd	dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset);
113116563Smdodd        sym = obj->symtab + ELF_R_SYM(rel->r_info);
114116563Smdodd        printf(rel_format,
115116563Smdodd		obj->strtab + sym->st_name,
116116582Smdodd		(u_long)rel->r_info, (u_long)rel->r_offset,
117116582Smdodd		(u_long)sym->st_value, (int)sym->st_size,
118116582Smdodd		dstaddr, (u_long)*dstaddr);
119116563Smdodd    }
120116563Smdodd    return;
121116563Smdodd}
122116563Smdodd
123116563Smdoddvoid
124116563Smdodddump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize)
125116563Smdodd{
126116563Smdodd    const Elf_Rela *rela;
127116563Smdodd    const Elf_Rela *relalim;
128116563Smdodd    const Elf_Sym *sym;
129116563Smdodd    Elf_Addr *dstaddr;
130116563Smdodd
131116563Smdodd    printf("%s", rel_header);
132116582Smdodd    relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
133116563Smdodd    for (rela = rela0; rela < relalim; rela++) {
134116563Smdodd	dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset);
135116563Smdodd        sym = obj->symtab + ELF_R_SYM(rela->r_info);
136116563Smdodd        printf(rel_format,
137116563Smdodd		obj->strtab + sym->st_name,
138116582Smdodd		(u_long)rela->r_info, (u_long)rela->r_offset,
139116582Smdodd		(u_long)sym->st_value, (int)sym->st_size,
140116582Smdodd		dstaddr, (u_long)*dstaddr);
141116563Smdodd    }
142116563Smdodd    return;
143116563Smdodd}
144