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