13124c3e0SJohn Polstra /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e6209940SPedro F. Giffuni *
43124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra.
53124c3e0SJohn Polstra * All rights reserved.
63124c3e0SJohn Polstra *
73124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without
83124c3e0SJohn Polstra * modification, are permitted provided that the following conditions
93124c3e0SJohn Polstra * are met:
103124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright
113124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer.
123124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright
133124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer in the
143124c3e0SJohn Polstra * documentation and/or other materials provided with the distribution.
153124c3e0SJohn Polstra *
163124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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"
370e9a2605SKonstantin Belousov #include "rtld_printf.h"
38c5d061c1SMatthew N. Dodd
39c5d061c1SMatthew N. Dodd static const char rel_header[] =
40c5d061c1SMatthew N. Dodd " symbol name r_info r_offset st_value st_size address value\n"
41c5d061c1SMatthew N. Dodd " ------------------------------------------------------------------------------\n";
42e2a47442SMatthew N. Dodd static const char rel_format[] = " %-25s %6lx %08lx %08lx %7d %10p %08lx\n";
433124c3e0SJohn Polstra
443124c3e0SJohn Polstra int debug = 0;
453124c3e0SJohn Polstra
463124c3e0SJohn Polstra void
debug_printf(const char * format,...)473124c3e0SJohn Polstra debug_printf(const char *format, ...)
483124c3e0SJohn Polstra {
493124c3e0SJohn Polstra if (debug) {
503124c3e0SJohn Polstra va_list ap;
513124c3e0SJohn Polstra va_start(ap, format);
523124c3e0SJohn Polstra
530e9a2605SKonstantin Belousov rtld_vfdprintf(STDERR_FILENO, format, ap);
540e9a2605SKonstantin Belousov rtld_fdputchar(STDERR_FILENO, '\n');
553124c3e0SJohn Polstra
563124c3e0SJohn Polstra va_end(ap);
573124c3e0SJohn Polstra }
583124c3e0SJohn Polstra }
59c5d061c1SMatthew N. Dodd
60c5d061c1SMatthew N. Dodd void
dump_relocations(Obj_Entry * obj0)61c5d061c1SMatthew N. Dodd dump_relocations (Obj_Entry *obj0)
62c5d061c1SMatthew N. Dodd {
63c5d061c1SMatthew N. Dodd Obj_Entry *obj;
64c5d061c1SMatthew N. Dodd
659fee0541SKonstantin Belousov for (obj = globallist_curr(obj0); obj != NULL;
669fee0541SKonstantin Belousov obj = globallist_next(obj)) {
67c5d061c1SMatthew N. Dodd dump_obj_relocations(obj);
68c5d061c1SMatthew N. Dodd }
69c5d061c1SMatthew N. Dodd }
70c5d061c1SMatthew N. Dodd
71c5d061c1SMatthew N. Dodd void
dump_obj_relocations(Obj_Entry * obj)72c5d061c1SMatthew N. Dodd dump_obj_relocations (Obj_Entry *obj)
73c5d061c1SMatthew N. Dodd {
74c5d061c1SMatthew N. Dodd
750e9a2605SKonstantin Belousov rtld_printf("Object \"%s\", relocbase %p\n", obj->path, obj->relocbase);
76c5d061c1SMatthew N. Dodd
77c5d061c1SMatthew N. Dodd if (obj->relsize) {
780e9a2605SKonstantin Belousov rtld_printf("Non-PLT Relocations: %ld\n",
79c5d061c1SMatthew N. Dodd (obj->relsize / sizeof(Elf_Rel)));
80c5d061c1SMatthew N. Dodd dump_Elf_Rel(obj, obj->rel, obj->relsize);
81c5d061c1SMatthew N. Dodd }
82c5d061c1SMatthew N. Dodd
83c5d061c1SMatthew N. Dodd if (obj->relasize) {
840e9a2605SKonstantin Belousov rtld_printf("Non-PLT Relocations with Addend: %ld\n",
85c5d061c1SMatthew N. Dodd (obj->relasize / sizeof(Elf_Rela)));
86c5d061c1SMatthew N. Dodd dump_Elf_Rela(obj, obj->rela, obj->relasize);
87c5d061c1SMatthew N. Dodd }
88c5d061c1SMatthew N. Dodd
89c5d061c1SMatthew N. Dodd if (obj->pltrelsize) {
900e9a2605SKonstantin Belousov rtld_printf("PLT Relocations: %ld\n",
91c5d061c1SMatthew N. Dodd (obj->pltrelsize / sizeof(Elf_Rel)));
92c5d061c1SMatthew N. Dodd dump_Elf_Rel(obj, obj->pltrel, obj->pltrelsize);
93c5d061c1SMatthew N. Dodd }
94c5d061c1SMatthew N. Dodd
95c5d061c1SMatthew N. Dodd if (obj->pltrelasize) {
960e9a2605SKonstantin Belousov rtld_printf("PLT Relocations with Addend: %ld\n",
97c5d061c1SMatthew N. Dodd (obj->pltrelasize / sizeof(Elf_Rela)));
98c5d061c1SMatthew N. Dodd dump_Elf_Rela(obj, obj->pltrela, obj->pltrelasize);
99c5d061c1SMatthew N. Dodd }
100c5d061c1SMatthew N. Dodd }
101c5d061c1SMatthew N. Dodd
102c5d061c1SMatthew N. Dodd void
dump_Elf_Rel(Obj_Entry * obj,const Elf_Rel * rel0,u_long relsize)103c5d061c1SMatthew N. Dodd dump_Elf_Rel (Obj_Entry *obj, const Elf_Rel *rel0, u_long relsize)
104c5d061c1SMatthew N. Dodd {
105c5d061c1SMatthew N. Dodd const Elf_Rel *rel;
106c5d061c1SMatthew N. Dodd const Elf_Rel *rellim;
107c5d061c1SMatthew N. Dodd const Elf_Sym *sym;
108c5d061c1SMatthew N. Dodd Elf_Addr *dstaddr;
109c5d061c1SMatthew N. Dodd
1100e9a2605SKonstantin Belousov rtld_putstr(rel_header);
111e2a47442SMatthew N. Dodd rellim = (const Elf_Rel *)((const char *)rel0 + relsize);
112c5d061c1SMatthew N. Dodd for (rel = rel0; rel < rellim; rel++) {
113c5d061c1SMatthew N. Dodd dstaddr = (Elf_Addr *)(obj->relocbase + rel->r_offset);
114c5d061c1SMatthew N. Dodd sym = obj->symtab + ELF_R_SYM(rel->r_info);
1150e9a2605SKonstantin Belousov rtld_printf(rel_format,
116c5d061c1SMatthew N. Dodd obj->strtab + sym->st_name,
117e2a47442SMatthew N. Dodd (u_long)rel->r_info, (u_long)rel->r_offset,
118e2a47442SMatthew N. Dodd (u_long)sym->st_value, (int)sym->st_size,
119e2a47442SMatthew N. Dodd dstaddr, (u_long)*dstaddr);
120c5d061c1SMatthew N. Dodd }
121c5d061c1SMatthew N. Dodd return;
122c5d061c1SMatthew N. Dodd }
123c5d061c1SMatthew N. Dodd
124c5d061c1SMatthew N. Dodd void
dump_Elf_Rela(Obj_Entry * obj,const Elf_Rela * rela0,u_long relasize)125c5d061c1SMatthew N. Dodd dump_Elf_Rela (Obj_Entry *obj, const Elf_Rela *rela0, u_long relasize)
126c5d061c1SMatthew N. Dodd {
127c5d061c1SMatthew N. Dodd const Elf_Rela *rela;
128c5d061c1SMatthew N. Dodd const Elf_Rela *relalim;
129c5d061c1SMatthew N. Dodd const Elf_Sym *sym;
130c5d061c1SMatthew N. Dodd Elf_Addr *dstaddr;
131c5d061c1SMatthew N. Dodd
1320e9a2605SKonstantin Belousov rtld_putstr(rel_header);
133e2a47442SMatthew N. Dodd relalim = (const Elf_Rela *)((const char *)rela0 + relasize);
134c5d061c1SMatthew N. Dodd for (rela = rela0; rela < relalim; rela++) {
135c5d061c1SMatthew N. Dodd dstaddr = (Elf_Addr *)(obj->relocbase + rela->r_offset);
136c5d061c1SMatthew N. Dodd sym = obj->symtab + ELF_R_SYM(rela->r_info);
1370e9a2605SKonstantin Belousov rtld_printf(rel_format,
138c5d061c1SMatthew N. Dodd obj->strtab + sym->st_name,
139e2a47442SMatthew N. Dodd (u_long)rela->r_info, (u_long)rela->r_offset,
140e2a47442SMatthew N. Dodd (u_long)sym->st_value, (int)sym->st_size,
141e2a47442SMatthew N. Dodd dstaddr, (u_long)*dstaddr);
142c5d061c1SMatthew N. Dodd }
143c5d061c1SMatthew N. Dodd return;
144c5d061c1SMatthew N. Dodd }
145