13124c3e0SJohn Polstra /*- 23124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra. 33124c3e0SJohn Polstra * All rights reserved. 43124c3e0SJohn Polstra * 53124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without 63124c3e0SJohn Polstra * modification, are permitted provided that the following conditions 73124c3e0SJohn Polstra * are met: 83124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright 93124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer. 103124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright 113124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer in the 123124c3e0SJohn Polstra * documentation and/or other materials provided with the distribution. 133124c3e0SJohn Polstra * 143124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 153124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 163124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 173124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 183124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 193124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 203124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 213124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 223124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 233124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 243124c3e0SJohn Polstra * 257f3dea24SPeter Wemm * $FreeBSD$ 263124c3e0SJohn Polstra */ 273124c3e0SJohn Polstra 283124c3e0SJohn Polstra /* 293124c3e0SJohn Polstra * Support for printing debugging messages. 303124c3e0SJohn Polstra */ 313124c3e0SJohn Polstra 323124c3e0SJohn Polstra #include <stdarg.h> 333124c3e0SJohn Polstra #include <stdio.h> 343124c3e0SJohn Polstra 353124c3e0SJohn Polstra #include "debug.h" 36c5d061c1SMatthew N. Dodd #include "rtld.h" 37c5d061c1SMatthew N. Dodd 38c5d061c1SMatthew N. Dodd static const char rel_header[] = 39c5d061c1SMatthew N. Dodd " symbol name r_info r_offset st_value st_size address value\n" 40c5d061c1SMatthew N. Dodd " ------------------------------------------------------------------------------\n"; 41c5d061c1SMatthew N. Dodd static const char rel_format[] = " %-25s %6x %08x %08x %7d %10p %08x\n"; 423124c3e0SJohn Polstra 433124c3e0SJohn Polstra int debug = 0; 443124c3e0SJohn Polstra 453124c3e0SJohn Polstra void 463124c3e0SJohn Polstra debug_printf(const char *format, ...) 473124c3e0SJohn Polstra { 483124c3e0SJohn Polstra if (debug) { 493124c3e0SJohn Polstra va_list ap; 503124c3e0SJohn Polstra va_start(ap, format); 513124c3e0SJohn Polstra 523124c3e0SJohn Polstra fflush(stdout); 533124c3e0SJohn Polstra vfprintf(stderr, format, ap); 543124c3e0SJohn Polstra putc('\n', stderr); 553124c3e0SJohn Polstra 563124c3e0SJohn Polstra va_end(ap); 573124c3e0SJohn Polstra } 583124c3e0SJohn Polstra } 59c5d061c1SMatthew N. Dodd 60c5d061c1SMatthew N. Dodd void 61c5d061c1SMatthew N. Dodd dump_relocations (Obj_Entry *obj0) 62c5d061c1SMatthew N. Dodd { 63c5d061c1SMatthew N. Dodd Obj_Entry *obj; 64c5d061c1SMatthew N. Dodd 65c5d061c1SMatthew N. Dodd for (obj = obj0; obj != NULL; obj = obj->next) { 66c5d061c1SMatthew N. Dodd dump_obj_relocations(obj); 67c5d061c1SMatthew N. Dodd } 68c5d061c1SMatthew N. Dodd } 69c5d061c1SMatthew N. Dodd 70c5d061c1SMatthew N. Dodd void 71c5d061c1SMatthew N. Dodd dump_obj_relocations (Obj_Entry *obj) 72c5d061c1SMatthew N. Dodd { 73c5d061c1SMatthew N. Dodd 74c5d061c1SMatthew N. Dodd printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase); 75c5d061c1SMatthew N. Dodd 76c5d061c1SMatthew N. Dodd if (obj->relsize) { 77c5d061c1SMatthew N. Dodd printf("Non-PLT Relocations: %ld\n", 78c5d061c1SMatthew N. Dodd (obj->relsize / sizeof(Elf_Rel))); 79c5d061c1SMatthew N. Dodd dump_Elf_Rel(obj, obj->rel, obj->relsize); 80c5d061c1SMatthew N. Dodd } 81c5d061c1SMatthew N. Dodd 82c5d061c1SMatthew N. Dodd if (obj->relasize) { 83c5d061c1SMatthew N. Dodd printf("Non-PLT Relocations with Addend: %ld\n", 84c5d061c1SMatthew N. Dodd (obj->relasize / sizeof(Elf_Rela))); 85c5d061c1SMatthew N. Dodd dump_Elf_Rela(obj, obj->rela, obj->relasize); 86c5d061c1SMatthew N. Dodd } 87c5d061c1SMatthew N. Dodd 88c5d061c1SMatthew N. Dodd if (obj->pltrelsize) { 89c5d061c1SMatthew N. Dodd printf("PLT Relocations: %ld\n", 90c5d061c1SMatthew N. Dodd (obj->pltrelsize / sizeof(Elf_Rel))); 91c5d061c1SMatthew N. Dodd dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize); 92c5d061c1SMatthew N. Dodd } 93c5d061c1SMatthew N. Dodd 94c5d061c1SMatthew N. Dodd if (obj->pltrelasize) { 95c5d061c1SMatthew N. Dodd printf("PLT Relocations with Addend: %ld\n", 96c5d061c1SMatthew N. Dodd (obj->pltrelasize / sizeof(Elf_Rela))); 97c5d061c1SMatthew N. Dodd dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize); 98c5d061c1SMatthew N. Dodd } 99c5d061c1SMatthew N. Dodd } 100c5d061c1SMatthew N. Dodd 101c5d061c1SMatthew N. Dodd void 102c5d061c1SMatthew N. Dodd dump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize) 103c5d061c1SMatthew N. Dodd { 104c5d061c1SMatthew N. Dodd const Elf_Rel *rel; 105c5d061c1SMatthew N. Dodd const Elf_Rel *rellim; 106c5d061c1SMatthew N. Dodd const Elf_Sym *sym; 107c5d061c1SMatthew N. Dodd Elf_Addr *dstaddr; 108c5d061c1SMatthew N. Dodd 109c5d061c1SMatthew N. Dodd printf("%s", rel_header); 110c5d061c1SMatthew N. Dodd rellim = (const Elf_Rel *)((char *)rel0 + relsize); 111c5d061c1SMatthew N. Dodd for (rel = rel0; rel < rellim; rel++) { 112c5d061c1SMatthew N. Dodd dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset); 113c5d061c1SMatthew N. Dodd sym = obj->symtab + ELF_R_SYM(rel->r_info); 114c5d061c1SMatthew N. Dodd printf(rel_format, 115c5d061c1SMatthew N. Dodd obj->strtab + sym->st_name, 116c5d061c1SMatthew N. Dodd rel->r_info, rel->r_offset, 117c5d061c1SMatthew N. Dodd sym->st_value, sym->st_size, 118c5d061c1SMatthew N. Dodd dstaddr, *dstaddr); 119c5d061c1SMatthew N. Dodd } 120c5d061c1SMatthew N. Dodd return; 121c5d061c1SMatthew N. Dodd } 122c5d061c1SMatthew N. Dodd 123c5d061c1SMatthew N. Dodd void 124c5d061c1SMatthew N. Dodd dump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize) 125c5d061c1SMatthew N. Dodd { 126c5d061c1SMatthew N. Dodd const Elf_Rela *rela; 127c5d061c1SMatthew N. Dodd const Elf_Rela *relalim; 128c5d061c1SMatthew N. Dodd const Elf_Sym *sym; 129c5d061c1SMatthew N. Dodd Elf_Addr *dstaddr; 130c5d061c1SMatthew N. Dodd 131c5d061c1SMatthew N. Dodd printf("%s", rel_header); 132c5d061c1SMatthew N. Dodd relalim = (const Elf_Rela *)((char *)rela0 + relasize); 133c5d061c1SMatthew N. Dodd for (rela = rela0; rela < relalim; rela++) { 134c5d061c1SMatthew N. Dodd dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset); 135c5d061c1SMatthew N. Dodd sym = obj->symtab + ELF_R_SYM(rela->r_info); 136c5d061c1SMatthew N. Dodd printf(rel_format, 137c5d061c1SMatthew N. Dodd obj->strtab + sym->st_name, 138c5d061c1SMatthew N. Dodd rela->r_info, rela->r_offset, 139c5d061c1SMatthew N. Dodd sym->st_value, sym->st_size, 140c5d061c1SMatthew N. Dodd dstaddr, *dstaddr); 141c5d061c1SMatthew N. Dodd } 142c5d061c1SMatthew N. Dodd return; 143c5d061c1SMatthew N. Dodd } 144