1 /* $OpenBSD: debug.c,v 1.4 2006/05/18 17:00:06 deraadt Exp $ */
2 /*
3  * Copyright (c) 2006 Dale Rahn <drahn@dalerahn.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/syslimits.h>
21 #include <sys/param.h>
22 #include <sys/mman.h>
23 #include <fcntl.h>
24 #include <nlist.h>
25 #include <elf_abi.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include "resolve.h"
32 #include "link.h"
33 #include "sod.h"
34 #ifndef __mips64__
35 #include "machine/reloc.h"
36 #endif
37 #include "prebind.h"
38 #include "prebind_struct.h"
39 
40 #ifdef DEBUG1
41 void
dump_info(struct elf_object * object)42 dump_info(struct elf_object *object)
43 {
44 	int numrel, numrela, i;
45 	const Elf_Sym	*symt;
46 	const char	*strt;
47 	Elf_Word *needed_list;
48 
49 	symt = object->dyn.symtab;
50 	strt = object->dyn.strtab;
51 
52 	for (i = 0; i < object->nchains; i++) {
53 		const Elf_Sym *sym = symt + i;
54 		char *type;
55 
56 		switch (ELF_ST_TYPE(sym->st_info)) {
57 		case STT_FUNC:
58 			type = "func";
59 			break;
60 		case STT_OBJECT:
61 			type = "object";
62 			break;
63 		case STT_NOTYPE:
64 			type = "notype";
65 			break;
66 		default:
67 			type = "UNKNOWN";
68 		}
69 		printf("symbol %d [%s] type %s value %x\n", i,
70 		    strt + sym->st_name,
71 		    type, sym->st_value);
72 	}
73 
74 	numrel = object->dyn.relsz / sizeof(Elf_Rel);
75 	numrela = object->dyn.relasz / sizeof(Elf_RelA);
76 	printf("numrel %d numrela %d\n", numrel, numrela);
77 
78 	printf("rel relocations:\n");
79 	for (i = 0; i < numrel ; i++) {
80 		Elf_Rel *rel = object->dyn.rel;
81 
82 		printf("%d: %x sym %x type %d\n", i, rel[i].r_offset,
83 		    ELF_R_SYM(rel[i].r_info), ELF_R_TYPE(rel[i].r_info));
84 	}
85 	printf("rela relocations:\n");
86 	for (i = 0; i < numrela ; i++) {
87 		Elf_RelA *rela = object->dyn.rela;
88 
89 		printf("%d: %x sym %x type %d\n", i, rela[i].r_offset,
90 		    ELF_R_SYM(rela[i].r_info), ELF_R_TYPE(rela[i].r_info));
91 	}
92 	needed_list = (Elf_Addr *)object->dyn.needed;
93 	for (i = 0; needed_list[i] != NULL; i++)
94 		printf("NEEDED %s\n", needed_list[i] + strt);
95 
96 }
97 #endif
98 
99 
100 void
elf_dump_footer(struct prebind_footer * footer)101 elf_dump_footer(struct prebind_footer *footer)
102 {
103 	printf("\nbase %llx\n", (long long)footer->prebind_base);
104 	printf("nameidx_idx %d\n", footer->nameidx_idx);
105 	printf("symcache_idx %d\n", footer->symcache_idx);
106 	printf("pltsymcache_idx %d\n", footer->pltsymcache_idx);
107 	printf("fixupcnt_idx %d\n", footer->fixupcnt_idx);
108 	printf("fixup_cnt %d\n", footer->fixup_cnt);
109 	printf("nametab_idx %d\n", footer->nametab_idx);
110 	printf("symcache_cnt %d\n", footer->symcache_cnt);
111 	printf("pltsymcache_cnt %d\n", footer->pltsymcache_cnt);
112 	printf("fixup_cnt %d\n", footer->fixup_cnt);
113 	printf("numlibs %d\n", footer->numlibs);
114 	printf("id0 %x\n", footer->id0);
115 	printf("id1 %x\n", footer->id1);
116 	printf("orig_size %lld\n", (long long)footer->orig_size);
117 	printf("version %d\n", footer->prebind_version);
118 	printf("bind_id %c%c%c%c\n", footer->bind_id[0],
119 	    footer->bind_id[1], footer->bind_id[2], footer->bind_id[3]);
120 }
121 
122 
123 void
dump_symcachetab(struct symcachetab * symcachetab,int symcache_cnt,struct elf_object * object,int id)124 dump_symcachetab(struct symcachetab *symcachetab, int symcache_cnt,
125     struct elf_object *object, int id)
126 {
127 	int i;
128 
129 	printf("symcache for %s\n", object->load_name);
130 	for (i = 0; i < symcache_cnt; i++) {
131 		printf("symidx %d: obj %d sym %d\n",
132 		    symcachetab[i].idx,
133 		    symcachetab[i].obj_idx,
134 		    symcachetab[i].sym_idx);
135 	}
136 }
137 
138 void
elf_print_prog_list(prog_list_ty * prog_list)139 elf_print_prog_list(prog_list_ty *prog_list)
140 {
141 	struct elf_object *object;
142 	struct proglist *pl;
143 
144 	TAILQ_FOREACH(pl, prog_list, list) {
145 		object = TAILQ_FIRST(&(pl->curbin_list))->object;
146 		printf("bin: %s\n", object->load_name);
147 		elf_print_curbin_list(pl);
148 	}
149 }
150 
151 void
elf_print_curbin_list(struct proglist * bin)152 elf_print_curbin_list(struct proglist *bin)
153 {
154 	struct objlist *ol;
155 
156 	TAILQ_FOREACH(ol, &(bin->curbin_list), list) {
157 		printf("\t%s\n", ol->object->load_name);
158 	}
159 }
160 
161