xref: /freebsd/contrib/elftoolchain/libdwarf/libdwarf_reloc.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1 /*-
2  * Copyright (c) 2010 Kai Wang
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: libdwarf_reloc.c 2948 2013-05-30 21:25:52Z kaiwang27 $");
30 
31 Dwarf_Unsigned
32 _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64)
33 {
34 
35 	assert(dbg != NULL);
36 
37 	switch (dbg->dbgp_isa) {
38 	case DW_ISA_X86:
39 		return (R_386_32);
40 	case DW_ISA_X86_64:
41 		return (is64 ? R_X86_64_64 : R_X86_64_32);
42 	case DW_ISA_SPARC:
43 		return (is64 ? R_SPARC_UA64 : R_SPARC_UA32);
44 	case DW_ISA_PPC:
45 		return (R_PPC_ADDR32);
46 	case DW_ISA_ARM:
47 		return (R_ARM_ABS32);
48 	case DW_ISA_MIPS:
49 		return (is64 ? R_MIPS_64 : R_MIPS_32);
50 	case DW_ISA_IA64:
51 		return (is64 ? R_IA_64_DIR64LSB : R_IA_64_DIR32LSB);
52 	default:
53 		break;
54 	}
55 	return (0);		/* NOT REACHED */
56 }
57 
58 int
59 _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type)
60 {
61 
62 	switch (dbg->dbg_machine) {
63 	case EM_NONE:
64 		break;
65 	case EM_ARM:
66 		if (rel_type == R_ARM_ABS32)
67 			return (4);
68 		break;
69 	case EM_386:
70 		if (rel_type == R_386_32)
71 			return (4);
72 		break;
73 	case EM_X86_64:
74 		if (rel_type == R_X86_64_32)
75 			return (4);
76 		else if (rel_type == R_X86_64_64)
77 			return (8);
78 		break;
79 	case EM_SPARC:
80 		if (rel_type == R_SPARC_UA32)
81 			return (4);
82 		else if (rel_type == R_SPARC_UA64)
83 			return (8);
84 		break;
85 	case EM_PPC:
86 		if (rel_type == R_PPC_ADDR32)
87 			return (4);
88 		break;
89 	case EM_MIPS:
90 		if (rel_type == R_MIPS_32)
91 			return (4);
92 		else if (rel_type == R_MIPS_64)
93 			return (8);
94 		break;
95 	case EM_IA_64:
96 		if (rel_type == R_IA_64_SECREL32LSB)
97 			return (4);
98 		else if (rel_type == R_IA_64_DIR64LSB)
99 			return (8);
100 		break;
101 	default:
102 		break;
103 	}
104 
105 	/* unknown relocation. */
106 	return (0);
107 }
108 
109 int
110 _dwarf_reloc_section_init(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp,
111     Dwarf_P_Section ref, Dwarf_Error *error)
112 {
113 	Dwarf_Rel_Section drs;
114 	char name[128];
115 	int pseudo;
116 
117 	assert(dbg != NULL && drsp != NULL && ref != NULL);
118 
119 	if ((drs = calloc(1, sizeof(struct _Dwarf_Rel_Section))) == NULL) {
120 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
121 		return (DW_DLE_MEMORY);
122 	}
123 
124 	drs->drs_ref = ref;
125 
126 	/*
127 	 * FIXME The logic here is most likely wrong. It should
128 	 * be the ISA that determines relocation type.
129 	 */
130 	if (dbg->dbgp_flags & DW_DLC_SIZE_64)
131 		drs->drs_addend = 1;
132 	else
133 		drs->drs_addend = 0;
134 
135 	if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS)
136 		pseudo = 1;
137 	else
138 		pseudo = 0;
139 
140 	snprintf(name, sizeof(name), "%s%s",
141 	    drs->drs_addend ? ".rela" : ".rel", ref->ds_name);
142 	if (_dwarf_section_init(dbg, &drs->drs_ds, name, pseudo, error) !=
143 	    DW_DLE_NONE) {
144 		free(drs);
145 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
146 		return (DW_DLE_MEMORY);
147 	}
148 
149 	STAILQ_INIT(&drs->drs_dre);
150 	STAILQ_INSERT_TAIL(&dbg->dbgp_drslist, drs, drs_next);
151 	dbg->dbgp_drscnt++;
152 	*drsp = drs;
153 
154 	return (DW_DLE_NONE);
155 }
156 
157 void
158 _dwarf_reloc_section_free(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp)
159 {
160 	Dwarf_Rel_Section drs, tdrs;
161 	Dwarf_Rel_Entry dre, tdre;
162 
163 	assert(dbg != NULL && drsp != NULL);
164 
165 	if (*drsp == NULL)
166 		return;
167 
168 	STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
169 		if (drs != *drsp)
170 			continue;
171 		STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
172 		    drs_next);
173 		STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
174 			STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
175 			    dre_next);
176 			free(dre);
177 		}
178 		if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0)
179 			_dwarf_section_free(dbg, &drs->drs_ds);
180 		else {
181 			if (drs->drs_ds->ds_name)
182 				free(drs->drs_ds->ds_name);
183 			free(drs->drs_ds);
184 		}
185 		free(drs);
186 		*drsp = NULL;
187 		dbg->dbgp_drscnt--;
188 		break;
189 	}
190 }
191 
192 int
193 _dwarf_reloc_entry_add(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
194     Dwarf_P_Section ds, unsigned char type, unsigned char length,
195     Dwarf_Unsigned offset, Dwarf_Unsigned symndx, Dwarf_Unsigned addend,
196     const char *secname, Dwarf_Error *error)
197 {
198 	Dwarf_Rel_Entry dre;
199 	Dwarf_Unsigned reloff;
200 	int ret;
201 
202 	assert(drs != NULL);
203 	assert(offset <= ds->ds_size);
204 	reloff = offset;
205 
206 	/*
207 	 * If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
208 	 * is used instead of ELfXX_Rela, we need to write the addend
209 	 * in the storage unit to be relocated. Otherwise write 0 in the
210 	 * storage unit and the addend will be written into relocation
211 	 * section later.
212 	 */
213 	if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
214 	    drs->drs_addend == 0)
215 		ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
216 		    addend, length, error);
217 	else
218 		ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
219 		    0, length, error);
220 	if (ret != DW_DLE_NONE)
221 		return (ret);
222 	if (offset > ds->ds_size)
223 		ds->ds_size = offset;
224 
225 	if ((dre = calloc(1, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
226 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
227 		return (DW_DLE_MEMORY);
228 	}
229 	STAILQ_INSERT_TAIL(&drs->drs_dre, dre, dre_next);
230 	dre->dre_type = type;
231 	dre->dre_length = length;
232 	dre->dre_offset = reloff;
233 	dre->dre_symndx = symndx;
234 	dre->dre_addend = addend;
235 	dre->dre_secname = secname;
236 	drs->drs_drecnt++;
237 
238 	return (DW_DLE_NONE);
239 }
240 
241 int
242 _dwarf_reloc_entry_add_pair(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
243     Dwarf_P_Section ds, unsigned char length, Dwarf_Unsigned offset,
244     Dwarf_Unsigned symndx, Dwarf_Unsigned esymndx, Dwarf_Unsigned symoff,
245     Dwarf_Unsigned esymoff, Dwarf_Error *error)
246 {
247 	Dwarf_Rel_Entry dre;
248 	Dwarf_Unsigned reloff;
249 	int ret;
250 
251 	assert(drs != NULL);
252 	assert(offset <= ds->ds_size);
253 	assert(dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS);
254 	reloff = offset;
255 
256 	/* Write net offset into section stream. */
257 	ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
258 	    esymoff - symoff, length, error);
259 	if (ret != DW_DLE_NONE)
260 		return (ret);
261 	if (offset > ds->ds_size)
262 		ds->ds_size = offset;
263 
264 	if ((dre = calloc(2, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
265 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
266 		return (DW_DLE_MEMORY);
267 	}
268 	STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[0], dre_next);
269 	STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[1], dre_next);
270 	dre[0].dre_type = dwarf_drt_first_of_length_pair;
271 	dre[0].dre_length = length;
272 	dre[0].dre_offset = reloff;
273 	dre[0].dre_symndx = symndx;
274 	dre[0].dre_addend = 0;
275 	dre[0].dre_secname = NULL;
276 	dre[1].dre_type = dwarf_drt_second_of_length_pair;
277 	dre[1].dre_length = length;
278 	dre[1].dre_offset = reloff;
279 	dre[1].dre_symndx = esymndx;
280 	dre[1].dre_addend = 0;
281 	dre[1].dre_secname = NULL;
282 	drs->drs_drecnt += 2;
283 
284 	return (DW_DLE_NONE);
285 }
286 
287 int
288 _dwarf_reloc_section_finalize(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
289     Dwarf_Error *error)
290 {
291 	Dwarf_P_Section ds;
292 	Dwarf_Unsigned unit;
293 	int ret, size;
294 
295 	assert(dbg != NULL && drs != NULL && drs->drs_ds != NULL &&
296 	    drs->drs_ref != NULL);
297 
298 	ds = drs->drs_ds;
299 
300 	/*
301 	 * Calculate the size (in bytes) of the relocation section.
302 	 */
303 	if (dbg->dbgp_flags & DW_DLC_SIZE_64)
304 		unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
305 	else
306 		unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
307 	assert(ds->ds_size == 0);
308 	size = drs->drs_drecnt * unit;
309 
310 	/*
311 	 * Discard this relocation section if there is no entry in it.
312 	 */
313 	if (size == 0) {
314 		_dwarf_reloc_section_free(dbg, &drs);
315 		return (DW_DLE_NONE);
316 	}
317 
318 	/*
319 	 * If we are under stream mode, realloc the section data block to
320 	 * this size.
321 	 */
322 	if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
323 		ds->ds_cap = size;
324 		if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
325 		    NULL) {
326 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
327 			return (DW_DLE_MEMORY);
328 		}
329 	}
330 
331 	/*
332 	 * Notify the application the creation of this relocation section.
333 	 * Note that the section link here should point to the .symtab
334 	 * section, we set it to 0 since we have no way to know .symtab
335 	 * section index.
336 	 */
337 	ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
338 	    drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
339 	    &ds->ds_symndx, NULL);
340 	if (ret < 0) {
341 		DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
342 		return (DW_DLE_ELF_SECT_ERR);
343 	}
344 	ds->ds_ndx = ret;
345 
346 	return (DW_DLE_NONE);
347 }
348 
349 int
350 _dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
351     Dwarf_Error *error)
352 {
353 	Dwarf_Rel_Entry dre;
354 	Dwarf_P_Section ds;
355 	Dwarf_Unsigned type;
356 	int ret;
357 
358 	assert((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0);
359 	assert(drs->drs_ds != NULL && drs->drs_ds->ds_size == 0);
360 	assert(!STAILQ_EMPTY(&drs->drs_dre));
361 	ds = drs->drs_ds;
362 
363 	STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
364 		assert(dre->dre_length == 4 || dre->dre_length == 8);
365 		type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
366 		if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
367 			/* Write r_offset (8 bytes) */
368 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
369 			    &ds->ds_size, dre->dre_offset, 8, error);
370 			if (ret != DW_DLE_NONE)
371 				return (ret);
372 			/* Write r_info (8 bytes) */
373 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
374 			    &ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
375 			    8, error);
376 			if (ret != DW_DLE_NONE)
377 				return (ret);
378 			/* Write r_addend (8 bytes) */
379 			if (drs->drs_addend) {
380 				ret = dbg->write_alloc(&ds->ds_data,
381 				    &ds->ds_cap, &ds->ds_size, dre->dre_addend,
382 				    8, error);
383 				if (ret != DW_DLE_NONE)
384 					return (ret);
385 			}
386 		} else {
387 			/* Write r_offset (4 bytes) */
388 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
389 			    &ds->ds_size, dre->dre_offset, 4, error);
390 			if (ret != DW_DLE_NONE)
391 				return (ret);
392 			/* Write r_info (4 bytes) */
393 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
394 			    &ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
395 			    4, error);
396 			if (ret != DW_DLE_NONE)
397 				return (ret);
398 			/* Write r_addend (4 bytes) */
399 			if (drs->drs_addend) {
400 				ret = dbg->write_alloc(&ds->ds_data,
401 				    &ds->ds_cap, &ds->ds_size, dre->dre_addend,
402 				    4, error);
403 				if (ret != DW_DLE_NONE)
404 					return (ret);
405 			}
406 		}
407 	}
408 	assert(ds->ds_size == ds->ds_cap);
409 
410 	return (DW_DLE_NONE);
411 }
412 
413 int
414 _dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
415 {
416 	Dwarf_Rel_Section drs;
417 	Dwarf_Rel_Entry dre;
418 	Dwarf_P_Section ds;
419 	int ret;
420 
421 	STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
422 		/*
423 		 * Update relocation entries: translate any section name
424 		 * reference to section symbol index.
425 		 */
426 		STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
427 			if (dre->dre_secname == NULL)
428 				continue;
429 			ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
430 			assert(ds != NULL && ds->ds_symndx != 0);
431 			dre->dre_symndx = ds->ds_symndx;
432 		}
433 
434 		/*
435 		 * Generate ELF relocation section if we are under stream
436 		 * mode.
437 		 */
438 		if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
439 			ret = _dwarf_reloc_section_gen(dbg, drs, error);
440 			if (ret != DW_DLE_NONE)
441 				return (ret);
442 		}
443 	}
444 
445 	return (DW_DLE_NONE);
446 }
447 
448 void
449 _dwarf_reloc_cleanup(Dwarf_P_Debug dbg)
450 {
451 	Dwarf_Rel_Section drs, tdrs;
452 	Dwarf_Rel_Entry dre, tdre;
453 
454 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
455 
456 	STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
457 		STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
458 		    drs_next);
459 		free(drs->drs_drd);
460 		STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
461 			STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
462 			    dre_next);
463 			free(dre);
464 		}
465 		if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
466 			if (drs->drs_ds) {
467 				if (drs->drs_ds->ds_name)
468 					free(drs->drs_ds->ds_name);
469 				free(drs->drs_ds);
470 			}
471 		}
472 		free(drs);
473 	}
474 	dbg->dbgp_drscnt = 0;
475 	dbg->dbgp_drspos = NULL;
476 }
477