| 1 |
/* |
| 2 |
Copyright (c) 2019, David Anderson |
| 3 |
All rights reserved. |
| 4 |
cc |
| 5 |
Redistribution and use in source and binary forms, with |
| 6 |
or without modification, are permitted provided that the |
| 7 |
following conditions are met: |
| 8 |
|
| 9 |
Redistributions of source code must retain the above |
| 10 |
copyright notice, this list of conditions and the following |
| 11 |
disclaimer. |
| 12 |
|
| 13 |
Redistributions in binary form must reproduce the above |
| 14 |
copyright notice, this list of conditions and the following |
| 15 |
disclaimer in the documentation and/or other materials |
| 16 |
provided with the distribution. |
| 17 |
|
| 18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
/* This file reads the parts of an Elf |
| 34 |
file appropriate to reading DWARF debugging data. |
| 35 |
Overview: |
| 36 |
_dwarf_elf_nlsetup() Does all elf setup. |
| 37 |
calls _dwarf_elf_access_init() |
| 38 |
calls _dwarf_elf_object_access_internals_init() |
| 39 |
Creates internals record 'M', |
| 40 |
dwarf_elf_object_access_internals_t |
| 41 |
Sets flags/data in internals record |
| 42 |
Loads elf object data needed later. |
| 43 |
Sets methods struct to access elf object. |
| 44 |
calls _dwarf_object_init_b() Creates Dwarf_Debug, independent |
| 45 |
of any elf code. |
| 46 |
Sets internals record into dbg. |
| 47 |
---------------------- |
| 48 |
_dwarf_destruct_elf_nlaccess(). This frees |
| 49 |
the elf internals record created in |
| 50 |
_dwarf_elf_object_access_internals_init() |
| 51 |
in case of errors during setup or when |
| 52 |
dwarf_finish() is called. Works safely for |
| 53 |
partially or fully set-up elf internals record. |
| 54 |
|
| 55 |
Other than in _dwarf_elf_nlsetup() the elf code |
| 56 |
knows nothing about Dwarf_Debug, and the rest of |
| 57 |
libdwarf knows nothing about the content of the |
| 58 |
object-type-specific (for Elf here) |
| 59 |
internals record. |
| 60 |
*/ |
| 61 |
|
| 62 |
#include <config.h> |
| 63 |
|
| 64 |
#include <stddef.h> /* size_t */ |
| 65 |
#include <stdlib.h> /* free() malloc() */ |
| 66 |
#include <stdio.h> /* debug printf */ |
| 67 |
#include <string.h> /* memset() strdup() strncmp() */ |
| 68 |
|
| 69 |
#include "dwarf.h" |
| 70 |
#include "libdwarf.h" |
| 71 |
#include "libdwarf_private.h" |
| 72 |
#include "dwarf_base_types.h" |
| 73 |
#include "dwarf_opaque.h" |
| 74 |
#include "dwarf_error.h" /* for _dwarf_error() declaration */ |
| 75 |
#include "dwarf_reading.h" |
| 76 |
#include "dwarf_memcpy_swap.h" |
| 77 |
#include "dwarf_object_read_common.h" |
| 78 |
#include "dwarf_object_detector.h" |
| 79 |
#include "dwarf_elfstructs.h" |
| 80 |
#include "dwarf_elf_defines.h" |
| 81 |
#include "dwarf_elf_rel_detector.h" |
| 82 |
#include "dwarf_elfread.h" |
| 83 |
|
| 84 |
#ifndef TYP |
| 85 |
#define TYP(n,l) char (n)[(l)] |
| 86 |
#endif /* TYPE */ |
| 87 |
|
| 88 |
#ifdef WORDS_BIGENDIAN |
| 89 |
#define READ_UNALIGNED_SAFE(dbg,dest, source, length) \ |
| 90 |
do { \ |
| 91 |
Dwarf_Unsigned _ltmp = 0; \ |
| 92 |
(dbg)->de_copy_word( (((char *)(&_ltmp)) + \ |
| 93 |
sizeof(_ltmp) - length),(source),(length)); \ |
| 94 |
dest = _ltmp; \ |
| 95 |
} while (0) |
| 96 |
|
| 97 |
#define WRITE_UNALIGNED_LOCAL(dbg,dest,source, srclength,len_out) \ |
| 98 |
{ \ |
| 99 |
(dbg)->de_copy_word((dest), \ |
| 100 |
((char *)(source)) +(srclength)-(len_out), \ |
| 101 |
(len_out)) ; \ |
| 102 |
} |
| 103 |
#else /* LITTLE ENDIAN */ |
| 104 |
#define READ_UNALIGNED_SAFE(dbg,dest, source, srclength) \ |
| 105 |
do { \ |
| 106 |
Dwarf_Unsigned _ltmp = 0; \ |
| 107 |
dbg->de_copy_word( (char *)(&_ltmp), \ |
| 108 |
(source), (srclength)) ; \ |
| 109 |
dest = _ltmp; \ |
| 110 |
} while (0) |
| 111 |
|
| 112 |
#define WRITE_UNALIGNED_LOCAL(dbg,dest,source, srclength,len_out) \ |
| 113 |
{ \ |
| 114 |
dbg->de_copy_word( (dest) , \ |
| 115 |
((char *)(source)) , \ |
| 116 |
(len_out)) ; \ |
| 117 |
} |
| 118 |
#endif /* *-ENDIAN */ |
| 119 |
|
| 120 |
static int |
| 121 |
_dwarf_elf_object_access_init( |
| 122 |
int fd, |
| 123 |
unsigned ftype, |
| 124 |
unsigned endian, |
| 125 |
unsigned offsetsize, |
| 126 |
size_t filesize, |
| 127 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 128 |
int *localerrnum); |
| 129 |
|
| 130 |
static Dwarf_Small elf_get_nolibelf_byte_order (void *obj) |
| 131 |
{ |
| 132 |
dwarf_elf_object_access_internals_t *elf = |
| 133 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 134 |
return (Dwarf_Small)elf->f_endian; |
| 135 |
} |
| 136 |
|
| 137 |
static Dwarf_Small elf_get_nolibelf_length_size (void *obj) |
| 138 |
{ |
| 139 |
dwarf_elf_object_access_internals_t *elf = |
| 140 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 141 |
return elf->f_offsetsize/8; |
| 142 |
} |
| 143 |
|
| 144 |
static Dwarf_Small elf_get_nolibelf_pointer_size (void *obj) |
| 145 |
{ |
| 146 |
dwarf_elf_object_access_internals_t *elf = |
| 147 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 148 |
return elf->f_pointersize/8; |
| 149 |
} |
| 150 |
|
| 151 |
static Dwarf_Unsigned elf_get_nolibelf_file_size(void *obj) |
| 152 |
{ |
| 153 |
dwarf_elf_object_access_internals_t *elf = |
| 154 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 155 |
return elf->f_filesize; |
| 156 |
} |
| 157 |
|
| 158 |
static Dwarf_Unsigned elf_get_nolibelf_section_count (void *obj) |
| 159 |
{ |
| 160 |
dwarf_elf_object_access_internals_t *elf = |
| 161 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 162 |
return elf->f_loc_shdr.g_count; |
| 163 |
} |
| 164 |
|
| 165 |
static int elf_get_nolibelf_section_info(void *obj, |
| 166 |
Dwarf_Unsigned section_index, |
| 167 |
Dwarf_Obj_Access_Section_a *return_section, |
| 168 |
int *error) |
| 169 |
{ |
| 170 |
dwarf_elf_object_access_internals_t *elf = |
| 171 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 172 |
|
| 173 |
(void)error; |
| 174 |
if (section_index < elf->f_loc_shdr.g_count) { |
| 175 |
struct generic_shdr *sp = 0; |
| 176 |
|
| 177 |
sp = elf->f_shdr + section_index; |
| 178 |
return_section->as_addr = sp->gh_addr; |
| 179 |
return_section->as_type = sp->gh_type; |
| 180 |
return_section->as_size = sp->gh_size; |
| 181 |
return_section->as_name = sp->gh_namestring; |
| 182 |
return_section->as_link = sp->gh_link; |
| 183 |
return_section->as_info = sp->gh_info; |
| 184 |
return_section->as_flags = sp->gh_flags; |
| 185 |
return_section->as_entrysize = sp->gh_entsize; |
| 186 |
return_section->as_offset = sp->gh_offset; |
| 187 |
return DW_DLV_OK; |
| 188 |
} |
| 189 |
return DW_DLV_NO_ENTRY; |
| 190 |
} |
| 191 |
|
| 192 |
static int |
| 193 |
elf_load_nolibelf_section (void *obj, Dwarf_Unsigned section_index, |
| 194 |
Dwarf_Small **return_data, int *error) |
| 195 |
{ |
| 196 |
/* Linux kernel read size limit 0x7ffff000, |
| 197 |
Without any good reason, limit our reads |
| 198 |
to a bit less. */ |
| 199 |
const Dwarf_Unsigned read_size_limit = 0x7ff00000; |
| 200 |
Dwarf_Unsigned read_offset = 0; |
| 201 |
Dwarf_Unsigned read_size = 0; |
| 202 |
Dwarf_Unsigned remaining_bytes = 0; |
| 203 |
Dwarf_Small * read_target = 0; |
| 204 |
dwarf_elf_object_access_internals_t *elf = |
| 205 |
(dwarf_elf_object_access_internals_t*)(obj); |
| 206 |
|
| 207 |
if (0 < section_index && |
| 208 |
section_index < elf->f_loc_shdr.g_count) { |
| 209 |
int res = 0; |
| 210 |
|
| 211 |
struct generic_shdr *sp = |
| 212 |
elf->f_shdr + section_index; |
| 213 |
if (sp->gh_content) { |
| 214 |
*return_data = (Dwarf_Small *)sp->gh_content; |
| 215 |
return DW_DLV_OK; |
| 216 |
} |
| 217 |
if (!sp->gh_size) { |
| 218 |
return DW_DLV_NO_ENTRY; |
| 219 |
} |
| 220 |
/* Guarding against bad values and |
| 221 |
against overflow */ |
| 222 |
if (sp->gh_size > elf->f_filesize || |
| 223 |
sp->gh_offset > elf->f_filesize || |
| 224 |
(sp->gh_size + sp->gh_offset) > |
| 225 |
elf->f_filesize) { |
| 226 |
*error = DW_DLE_ELF_SECTION_ERROR; |
| 227 |
return DW_DLV_ERROR; |
| 228 |
} |
| 229 |
|
| 230 |
sp->gh_content = malloc((size_t)sp->gh_size); |
| 231 |
if (!sp->gh_content) { |
| 232 |
*error = DW_DLE_ALLOC_FAIL; |
| 233 |
return DW_DLV_ERROR; |
| 234 |
} |
| 235 |
/* Linux has a 2GB limit on read size. |
| 236 |
So break this into 2gb pieces. */ |
| 237 |
remaining_bytes = sp->gh_size; |
| 238 |
read_size = remaining_bytes; |
| 239 |
read_offset = sp->gh_offset; |
| 240 |
read_target = (Dwarf_Small*)sp->gh_content; |
| 241 |
for ( ; remaining_bytes > 0; read_size = remaining_bytes ) { |
| 242 |
if (read_size > read_size_limit) { |
| 243 |
read_size = read_size_limit; |
| 244 |
} |
| 245 |
res = RRMOA(elf->f_fd, |
| 246 |
(void *)read_target, read_offset, |
| 247 |
read_size, |
| 248 |
elf->f_filesize, error); |
| 249 |
if (res != DW_DLV_OK) { |
| 250 |
free(sp->gh_content); |
| 251 |
sp->gh_content = 0; |
| 252 |
return res; |
| 253 |
} |
| 254 |
remaining_bytes -= read_size; |
| 255 |
read_offset += read_size; |
| 256 |
read_target += read_size; |
| 257 |
} |
| 258 |
*return_data = (Dwarf_Small *)sp->gh_content; |
| 259 |
return DW_DLV_OK; |
| 260 |
} |
| 261 |
return DW_DLV_NO_ENTRY; |
| 262 |
} |
| 263 |
|
| 264 |
#define MATCH_REL_SEC(i_,s_,r_) \ |
| 265 |
if ((i_) == (s_).dss_index) { \ |
| 266 |
*(r_) = &(s_); \ |
| 267 |
return DW_DLV_OK; \ |
| 268 |
} |
| 269 |
|
| 270 |
static int |
| 271 |
find_section_to_relocate(Dwarf_Debug dbg,Dwarf_Unsigned section_index, |
| 272 |
struct Dwarf_Section_s **relocatablesec, int *error) |
| 273 |
{ |
| 274 |
MATCH_REL_SEC(section_index,dbg->de_debug_info,relocatablesec); |
| 275 |
MATCH_REL_SEC(section_index,dbg->de_debug_abbrev,relocatablesec); |
| 276 |
MATCH_REL_SEC(section_index,dbg->de_debug_line,relocatablesec); |
| 277 |
MATCH_REL_SEC(section_index,dbg->de_debug_loc,relocatablesec); |
| 278 |
MATCH_REL_SEC(section_index,dbg->de_debug_aranges,relocatablesec); |
| 279 |
MATCH_REL_SEC(section_index,dbg->de_debug_macinfo,relocatablesec); |
| 280 |
MATCH_REL_SEC(section_index,dbg->de_debug_pubnames, |
| 281 |
relocatablesec); |
| 282 |
MATCH_REL_SEC(section_index,dbg->de_debug_names, |
| 283 |
relocatablesec); |
| 284 |
MATCH_REL_SEC(section_index,dbg->de_debug_ranges,relocatablesec); |
| 285 |
MATCH_REL_SEC(section_index,dbg->de_debug_frame, |
| 286 |
relocatablesec); |
| 287 |
MATCH_REL_SEC(section_index,dbg->de_debug_frame_eh_gnu, |
| 288 |
relocatablesec); |
| 289 |
MATCH_REL_SEC(section_index,dbg->de_debug_pubtypes, |
| 290 |
relocatablesec); |
| 291 |
MATCH_REL_SEC(section_index,dbg->de_debug_funcnames, |
| 292 |
relocatablesec); |
| 293 |
MATCH_REL_SEC(section_index,dbg->de_debug_typenames, |
| 294 |
relocatablesec); |
| 295 |
MATCH_REL_SEC(section_index,dbg->de_debug_varnames, |
| 296 |
relocatablesec); |
| 297 |
MATCH_REL_SEC(section_index,dbg->de_debug_weaknames, |
| 298 |
relocatablesec); |
| 299 |
MATCH_REL_SEC(section_index,dbg->de_debug_types,relocatablesec); |
| 300 |
MATCH_REL_SEC(section_index,dbg->de_debug_macro,relocatablesec); |
| 301 |
MATCH_REL_SEC(section_index,dbg->de_debug_rnglists, |
| 302 |
relocatablesec); |
| 303 |
MATCH_REL_SEC(section_index,dbg->de_debug_loclists, |
| 304 |
relocatablesec); |
| 305 |
MATCH_REL_SEC(section_index,dbg->de_debug_aranges, |
| 306 |
relocatablesec); |
| 307 |
MATCH_REL_SEC(section_index,dbg->de_debug_sup, |
| 308 |
relocatablesec); |
| 309 |
MATCH_REL_SEC(section_index,dbg->de_debug_str_offsets, |
| 310 |
relocatablesec); |
| 311 |
MATCH_REL_SEC(section_index,dbg->de_debug_addr,relocatablesec); |
| 312 |
MATCH_REL_SEC(section_index,dbg->de_debug_gnu_pubnames, |
| 313 |
relocatablesec); |
| 314 |
MATCH_REL_SEC(section_index,dbg->de_debug_gnu_pubtypes, |
| 315 |
relocatablesec); |
| 316 |
/* dbg-> de_debug_tu_index,reloctablesec); */ |
| 317 |
/* dbg-> de_debug_cu_index,reloctablesec); */ |
| 318 |
/* dbg-> de_debug_gdbindex,reloctablesec); */ |
| 319 |
/* dbg-> de_debug_str,syms); */ |
| 320 |
/* de_elf_symtab,syms); */ |
| 321 |
/* de_elf_strtab,syms); */ |
| 322 |
*error = DW_DLE_RELOC_SECTION_MISMATCH; |
| 323 |
return DW_DLV_ERROR; |
| 324 |
} |
| 325 |
|
| 326 |
/* Returns DW_DLV_OK if it works, else DW_DLV_ERROR. |
| 327 |
The caller may decide to ignore the errors or report them. */ |
| 328 |
static int |
| 329 |
update_entry(Dwarf_Debug dbg, |
| 330 |
dwarf_elf_object_access_internals_t*obj, |
| 331 |
struct generic_rela *rela, |
| 332 |
Dwarf_Small *target_section, |
| 333 |
Dwarf_Unsigned target_section_size, |
| 334 |
int *error) |
| 335 |
{ |
| 336 |
unsigned int type = 0; |
| 337 |
unsigned int sym_idx = 0; |
| 338 |
Dwarf_Unsigned offset = 0; |
| 339 |
Dwarf_Signed addend = 0; |
| 340 |
Dwarf_Half reloc_size = 0; |
| 341 |
Dwarf_Half machine = (Dwarf_Half)obj->f_machine; |
| 342 |
struct generic_symentry *symp = 0; |
| 343 |
int is_rela = rela->gr_is_rela; |
| 344 |
|
| 345 |
offset = rela->gr_offset; |
| 346 |
addend = rela->gr_addend; |
| 347 |
type = (unsigned int)rela->gr_type; |
| 348 |
sym_idx = (unsigned int)rela->gr_sym; |
| 349 |
if (sym_idx >= obj->f_loc_symtab.g_count) { |
| 350 |
*error = DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD; |
| 351 |
return DW_DLV_ERROR; |
| 352 |
} |
| 353 |
symp = obj->f_symtab + sym_idx; |
| 354 |
if (offset >= target_section_size) { |
| 355 |
/* If offset really big, any add will overflow. |
| 356 |
So lets stop early if offset is corrupt. */ |
| 357 |
*error = DW_DLE_RELOC_INVALID; |
| 358 |
return DW_DLV_ERROR; |
| 359 |
} |
| 360 |
/* Determine relocation size */ |
| 361 |
if (_dwarf_is_32bit_abs_reloc(type, machine)) { |
| 362 |
reloc_size = 4; |
| 363 |
} else if (_dwarf_is_64bit_abs_reloc(type, machine)) { |
| 364 |
reloc_size = 8; |
| 365 |
} else if (!type) { |
| 366 |
/* There is nothing to do. , this is the case such as |
| 367 |
R_AARCH64_NONE and R_X86_64_NONE and the other machine |
| 368 |
cases have it too. Most object files do not have |
| 369 |
any relocation records of type R_<machine>_NONE. */ |
| 370 |
return DW_DLV_OK; |
| 371 |
} else { |
| 372 |
*error = DW_DLE_RELOC_SECTION_RELOC_TARGET_SIZE_UNKNOWN; |
| 373 |
return DW_DLV_ERROR; |
| 374 |
} |
| 375 |
if ( (offset + reloc_size) < offset) { |
| 376 |
/* Another check for overflow. */ |
| 377 |
*error = DW_DLE_RELOC_INVALID; |
| 378 |
return DW_DLV_ERROR; |
| 379 |
} |
| 380 |
if ( (offset + reloc_size) > target_section_size) { |
| 381 |
*error = DW_DLE_RELOC_INVALID; |
| 382 |
return DW_DLV_ERROR; |
| 383 |
} |
| 384 |
/* Assuming we do not need to do a READ_UNALIGNED here |
| 385 |
at target_section + offset and add its value to |
| 386 |
outval. Some ABIs say no read (for example MIPS), |
| 387 |
but if some do then which ones? */ |
| 388 |
{ /* .rel. (addend is 0), or .rela. */ |
| 389 |
Dwarf_Small *targ = target_section+offset; |
| 390 |
Dwarf_Unsigned presentval = 0; |
| 391 |
Dwarf_Unsigned outval = 0; |
| 392 |
|
| 393 |
if (!is_rela) { |
| 394 |
READ_UNALIGNED_SAFE(dbg,presentval, |
| 395 |
targ,(unsigned long)reloc_size); |
| 396 |
} |
| 397 |
/* There is no addend in .rel. |
| 398 |
Normally presentval is correct |
| 399 |
and st_value will be zero. |
| 400 |
But a few compilers have |
| 401 |
presentval zero and st_value set. */ |
| 402 |
outval = presentval + symp->gs_value + addend; |
| 403 |
WRITE_UNALIGNED_LOCAL(dbg,targ, |
| 404 |
&outval,sizeof(outval),(unsigned long)reloc_size); |
| 405 |
} |
| 406 |
return DW_DLV_OK; |
| 407 |
} |
| 408 |
|
| 409 |
/* Somewhat arbitrarily, we attempt to apply all the |
| 410 |
relocations we can |
| 411 |
and still notify the caller of at least one error if we found |
| 412 |
any errors. */ |
| 413 |
|
| 414 |
static int |
| 415 |
apply_rela_entries( |
| 416 |
Dwarf_Debug dbg, |
| 417 |
/* Section_index of the relocation section, .rela entries */ |
| 418 |
Dwarf_Unsigned r_section_index, |
| 419 |
dwarf_elf_object_access_internals_t*obj, |
| 420 |
/* relocatablesec is the .debug_info(etc) in Dwarf_Debug */ |
| 421 |
struct Dwarf_Section_s * relocatablesec, |
| 422 |
int *error) |
| 423 |
{ |
| 424 |
int return_res = DW_DLV_OK; |
| 425 |
struct generic_shdr * rels_shp = 0; |
| 426 |
Dwarf_Unsigned relcount; |
| 427 |
Dwarf_Unsigned i = 0; |
| 428 |
|
| 429 |
if (r_section_index >= obj->f_loc_shdr.g_count) { |
| 430 |
*error = DW_DLE_SECTION_INDEX_BAD; |
| 431 |
return DW_DLV_ERROR; |
| 432 |
} |
| 433 |
rels_shp = obj->f_shdr + r_section_index; |
| 434 |
relcount = rels_shp->gh_relcount; |
| 435 |
if (obj->f_ehdr->ge_type != ET_REL) { |
| 436 |
/* No relocations to do */ |
| 437 |
return DW_DLV_OK; |
| 438 |
} |
| 439 |
if (!relcount) { |
| 440 |
/* Nothing to do. */ |
| 441 |
return DW_DLV_OK; |
| 442 |
} |
| 443 |
if (!rels_shp->gh_rels) { |
| 444 |
/* something wrong. */ |
| 445 |
*error = DW_DLE_RELOCS_ERROR; |
| 446 |
return DW_DLV_ERROR; |
| 447 |
} |
| 448 |
for (i = 0; i < relcount; i++) { |
| 449 |
int res = update_entry(dbg,obj, |
| 450 |
rels_shp->gh_rels+i, |
| 451 |
relocatablesec->dss_data, |
| 452 |
relocatablesec->dss_size, |
| 453 |
error); |
| 454 |
if (res != DW_DLV_OK) { |
| 455 |
/* We try to keep going, not stop. */ |
| 456 |
return_res = res; |
| 457 |
} |
| 458 |
} |
| 459 |
return return_res; |
| 460 |
} |
| 461 |
|
| 462 |
/* Find the section data in dbg and find all the relevant |
| 463 |
sections. Then do relocations. |
| 464 |
|
| 465 |
section_index is the index of a .debug_info (for example) |
| 466 |
so we have to find the section(s) with relocations |
| 467 |
targeting section_index. |
| 468 |
Normally there is exactly one such, though. |
| 469 |
*/ |
| 470 |
static int |
| 471 |
elf_relocations_nolibelf(void* obj_in, |
| 472 |
Dwarf_Unsigned section_index, |
| 473 |
Dwarf_Debug dbg, |
| 474 |
int* error) |
| 475 |
{ |
| 476 |
int res = DW_DLV_ERROR; |
| 477 |
dwarf_elf_object_access_internals_t*obj = 0; |
| 478 |
struct Dwarf_Section_s * relocatablesec = 0; |
| 479 |
Dwarf_Unsigned section_with_reloc_records = 0; |
| 480 |
|
| 481 |
if (section_index == 0) { |
| 482 |
return DW_DLV_NO_ENTRY; |
| 483 |
} |
| 484 |
obj = (dwarf_elf_object_access_internals_t*)obj_in; |
| 485 |
|
| 486 |
/* The section to relocate must already be loaded into memory. |
| 487 |
This just turns section_index into a pointer |
| 488 |
to a de_debug_info or other section record in |
| 489 |
Dwarf_Debug. */ |
| 490 |
res = find_section_to_relocate(dbg, section_index, |
| 491 |
&relocatablesec, error); |
| 492 |
if (res != DW_DLV_OK) { |
| 493 |
return res; |
| 494 |
} |
| 495 |
/* Now we know the Dwarf_Section_s section |
| 496 |
we need to relocate. |
| 497 |
So lets find the rela section(s) targeting this. |
| 498 |
*/ |
| 499 |
|
| 500 |
/* Sun and possibly others do not always set |
| 501 |
sh_link in .debug_* sections. |
| 502 |
So we cannot do full consistency checks. |
| 503 |
FIXME: This approach assumes there is only one |
| 504 |
relocation section applying to section section_index! */ |
| 505 |
section_with_reloc_records = relocatablesec->dss_reloc_index; |
| 506 |
if (!section_with_reloc_records) { |
| 507 |
/* Something is wrong. */ |
| 508 |
*error = DW_DLE_RELOC_SECTION_MISSING_INDEX; |
| 509 |
return DW_DLV_ERROR; |
| 510 |
} |
| 511 |
/* The relocations, if they exist, have been loaded. */ |
| 512 |
/* The symtab was already loaded. */ |
| 513 |
if (!obj->f_symtab || !obj->f_symtab_sect_strings) { |
| 514 |
*error = DW_DLE_DEBUG_SYMTAB_ERR; |
| 515 |
return DW_DLV_ERROR; |
| 516 |
} |
| 517 |
if (obj->f_symtab_sect_index != relocatablesec->dss_reloc_link) { |
| 518 |
/* Something is wrong. */ |
| 519 |
*error = DW_DLE_RELOC_MISMATCH_RELOC_INDEX; |
| 520 |
return DW_DLV_ERROR; |
| 521 |
} |
| 522 |
/* We have all the data we need in memory. */ |
| 523 |
/* Now we apply the relocs in section_with_reloc_records to the |
| 524 |
target, relocablesec */ |
| 525 |
res = apply_rela_entries(dbg, |
| 526 |
section_with_reloc_records, |
| 527 |
obj, relocatablesec,error); |
| 528 |
return res; |
| 529 |
} |
| 530 |
|
| 531 |
void |
| 532 |
_dwarf_destruct_elf_nlaccess( |
| 533 |
struct Dwarf_Obj_Access_Interface_a_s *aip) |
| 534 |
{ |
| 535 |
dwarf_elf_object_access_internals_t *ep = 0; |
| 536 |
struct generic_shdr *shp = 0; |
| 537 |
Dwarf_Unsigned shcount = 0; |
| 538 |
Dwarf_Unsigned i = 0; |
| 539 |
|
| 540 |
ep = (dwarf_elf_object_access_internals_t *)aip->ai_object; |
| 541 |
free(ep->f_ehdr); |
| 542 |
shp = ep->f_shdr; |
| 543 |
shcount = ep->f_loc_shdr.g_count; |
| 544 |
for (i = 0; i < shcount; ++i,++shp) { |
| 545 |
free(shp->gh_rels); |
| 546 |
shp->gh_rels = 0; |
| 547 |
free(shp->gh_content); |
| 548 |
shp->gh_content = 0; |
| 549 |
free(shp->gh_sht_group_array); |
| 550 |
shp->gh_sht_group_array = 0; |
| 551 |
shp->gh_sht_group_array_count = 0; |
| 552 |
} |
| 553 |
free(ep->f_shdr); |
| 554 |
ep->f_loc_shdr.g_count = 0; |
| 555 |
free(ep->f_phdr); |
| 556 |
free(ep->f_elf_shstrings_data); |
| 557 |
free(ep->f_dynamic); |
| 558 |
free(ep->f_symtab_sect_strings); |
| 559 |
free(ep->f_dynsym_sect_strings); |
| 560 |
free(ep->f_symtab); |
| 561 |
free(ep->f_dynsym); |
| 562 |
|
| 563 |
/* if TRUE close f_fd on destruct.*/ |
| 564 |
if (ep->f_destruct_close_fd) { |
| 565 |
_dwarf_closer(ep->f_fd); |
| 566 |
} |
| 567 |
ep->f_ident[0] = 'X'; |
| 568 |
free(ep->f_path); |
| 569 |
free(ep); |
| 570 |
free(aip); |
| 571 |
} |
| 572 |
|
| 573 |
int |
| 574 |
_dwarf_elf_nlsetup(int fd, |
| 575 |
char *true_path, |
| 576 |
unsigned ftype, |
| 577 |
unsigned endian, |
| 578 |
unsigned offsetsize, |
| 579 |
size_t filesize, |
| 580 |
unsigned groupnumber, |
| 581 |
Dwarf_Handler errhand, |
| 582 |
Dwarf_Ptr errarg, |
| 583 |
Dwarf_Debug *dbg,Dwarf_Error *error) |
| 584 |
{ |
| 585 |
Dwarf_Obj_Access_Interface_a *binary_interface = 0; |
| 586 |
dwarf_elf_object_access_internals_t *intfc = 0; |
| 587 |
int res = DW_DLV_OK; |
| 588 |
int localerrnum = 0; |
| 589 |
|
| 590 |
res = _dwarf_elf_object_access_init( |
| 591 |
fd, |
| 592 |
ftype,endian,offsetsize,filesize, |
| 593 |
&binary_interface, |
| 594 |
&localerrnum); |
| 595 |
if (res != DW_DLV_OK) { |
| 596 |
if (res == DW_DLV_NO_ENTRY) { |
| 597 |
return res; |
| 598 |
} |
| 599 |
_dwarf_error(NULL, error, localerrnum); |
| 600 |
return DW_DLV_ERROR; |
| 601 |
} |
| 602 |
/* allocates and initializes Dwarf_Debug, |
| 603 |
generic code */ |
| 604 |
res = dwarf_object_init_b(binary_interface, errhand, errarg, |
| 605 |
groupnumber, dbg, error); |
| 606 |
if (res != DW_DLV_OK){ |
| 607 |
_dwarf_destruct_elf_nlaccess(binary_interface); |
| 608 |
return res; |
| 609 |
} |
| 610 |
intfc = binary_interface->ai_object; |
| 611 |
intfc->f_path = strdup(true_path); |
| 612 |
(*dbg)->de_obj_machine = intfc->f_machine; |
| 613 |
(*dbg)->de_obj_flags = intfc->f_flags; |
| 614 |
return res; |
| 615 |
} |
| 616 |
|
| 617 |
/* dwarf_elf_access method table for use with non-libelf. |
| 618 |
See also the methods table in dwarf_elf_access.c for libelf. |
| 619 |
*/ |
| 620 |
static Dwarf_Obj_Access_Methods_a const elf_nlmethods = { |
| 621 |
elf_get_nolibelf_section_info, |
| 622 |
elf_get_nolibelf_byte_order, |
| 623 |
elf_get_nolibelf_length_size, |
| 624 |
elf_get_nolibelf_pointer_size, |
| 625 |
elf_get_nolibelf_file_size, |
| 626 |
elf_get_nolibelf_section_count, |
| 627 |
elf_load_nolibelf_section, |
| 628 |
elf_relocations_nolibelf |
| 629 |
}; |
| 630 |
|
| 631 |
/* On any error this frees internals argument. */ |
| 632 |
static int |
| 633 |
_dwarf_elf_object_access_internals_init( |
| 634 |
dwarf_elf_object_access_internals_t * internals, |
| 635 |
int fd, |
| 636 |
unsigned ftype, |
| 637 |
unsigned endian, |
| 638 |
unsigned offsetsize, |
| 639 |
size_t filesize, |
| 640 |
int *errcode) |
| 641 |
{ |
| 642 |
dwarf_elf_object_access_internals_t * intfc = internals; |
| 643 |
Dwarf_Unsigned i = 0; |
| 644 |
struct Dwarf_Obj_Access_Interface_a_s *localdoas; |
| 645 |
int res = 0; |
| 646 |
|
| 647 |
/* Must malloc as _dwarf_destruct_elf_access() |
| 648 |
forces that due to other uses. */ |
| 649 |
localdoas = (struct Dwarf_Obj_Access_Interface_a_s *) |
| 650 |
malloc(sizeof(struct Dwarf_Obj_Access_Interface_a_s)); |
| 651 |
if (!localdoas) { |
| 652 |
free(internals); |
| 653 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 654 |
return DW_DLV_ERROR; |
| 655 |
} |
| 656 |
memset(localdoas,0,sizeof(struct Dwarf_Obj_Access_Interface_a_s)); |
| 657 |
/* E is used with libelf. F with this elf reader. */ |
| 658 |
intfc->f_ident[0] = 'F'; |
| 659 |
intfc->f_ident[1] = '1'; |
| 660 |
intfc->f_fd = fd; |
| 661 |
intfc->f_is_64bit = ((offsetsize==64)?TRUE:FALSE); |
| 662 |
intfc->f_offsetsize = (Dwarf_Small)offsetsize; |
| 663 |
intfc->f_pointersize = (Dwarf_Small)offsetsize; |
| 664 |
intfc->f_filesize = filesize; |
| 665 |
intfc->f_ftype = ftype; |
| 666 |
intfc->f_destruct_close_fd = FALSE; |
| 667 |
|
| 668 |
#ifdef WORDS_BIGENDIAN |
| 669 |
if (endian == DW_END_little ) { |
| 670 |
intfc->f_copy_word = _dwarf_memcpy_swap_bytes; |
| 671 |
intfc->f_endian = DW_END_little; |
| 672 |
} else { |
| 673 |
intfc->f_copy_word = _dwarf_memcpy_noswap_bytes; |
| 674 |
intfc->f_endian = DW_END_big; |
| 675 |
} |
| 676 |
#else /* LITTLE ENDIAN */ |
| 677 |
if (endian == DW_END_little ) { |
| 678 |
intfc->f_copy_word = _dwarf_memcpy_noswap_bytes; |
| 679 |
intfc->f_endian = DW_END_little; |
| 680 |
} else { |
| 681 |
intfc->f_copy_word = _dwarf_memcpy_swap_bytes; |
| 682 |
intfc->f_endian = DW_END_big; |
| 683 |
} |
| 684 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 685 |
/* The following sets f_machine. */ |
| 686 |
res = _dwarf_load_elf_header(intfc,errcode); |
| 687 |
if (res != DW_DLV_OK) { |
| 688 |
localdoas->ai_object = intfc; |
| 689 |
localdoas->ai_methods = 0; |
| 690 |
_dwarf_destruct_elf_nlaccess(localdoas); |
| 691 |
localdoas = 0; |
| 692 |
return res; |
| 693 |
} |
| 694 |
/* Not loading progheaders */ |
| 695 |
res = _dwarf_load_elf_sectheaders(intfc,errcode); |
| 696 |
if (res != DW_DLV_OK) { |
| 697 |
localdoas->ai_object = intfc; |
| 698 |
localdoas->ai_methods = 0; |
| 699 |
_dwarf_destruct_elf_nlaccess(localdoas); |
| 700 |
localdoas = 0; |
| 701 |
return res; |
| 702 |
} |
| 703 |
/* We are not looking at symbol strings for now. */ |
| 704 |
res = _dwarf_load_elf_symstr(intfc,errcode); |
| 705 |
if (res == DW_DLV_ERROR) { |
| 706 |
localdoas->ai_object = intfc; |
| 707 |
localdoas->ai_methods = 0; |
| 708 |
_dwarf_destruct_elf_nlaccess(localdoas); |
| 709 |
localdoas = 0; |
| 710 |
return res; |
| 711 |
} |
| 712 |
res = _dwarf_load_elf_symtab_symbols(intfc,errcode); |
| 713 |
if (res == DW_DLV_ERROR) { |
| 714 |
localdoas->ai_object = intfc; |
| 715 |
localdoas->ai_methods = 0; |
| 716 |
_dwarf_destruct_elf_nlaccess(localdoas); |
| 717 |
localdoas = 0; |
| 718 |
return res; |
| 719 |
} |
| 720 |
for ( i = 1; i < intfc->f_loc_shdr.g_count; ++i) { |
| 721 |
struct generic_shdr *shp = 0; |
| 722 |
Dwarf_Unsigned section_type = 0; |
| 723 |
enum RelocRela localrel = RelocIsRela; |
| 724 |
|
| 725 |
shp = intfc->f_shdr +i; |
| 726 |
section_type = shp->gh_type; |
| 727 |
if (!shp->gh_namestring) { |
| 728 |
/* A serious error which we ignore here |
| 729 |
as it will be caught elsewhere |
| 730 |
if necessary. */ |
| 731 |
continue; |
| 732 |
} else if (section_type == SHT_REL) { |
| 733 |
localrel = RelocIsRel; |
| 734 |
} else if (section_type == SHT_RELA) { |
| 735 |
localrel = RelocIsRela; |
| 736 |
} else if (!strncmp(".rel.",shp->gh_namestring,5)) { |
| 737 |
localrel = RelocIsRel; |
| 738 |
} else if (!strncmp(".rela.",shp->gh_namestring,6)) { |
| 739 |
localrel = RelocIsRela; |
| 740 |
} else { |
| 741 |
continue; |
| 742 |
} |
| 743 |
/* ASSERT: local rel is either RelocIsRel or |
| 744 |
RelocIsRela. Never any other value. */ |
| 745 |
/* Possibly we should check if the target section |
| 746 |
is one we care about before loading rela |
| 747 |
FIXME */ |
| 748 |
res = _dwarf_load_elf_relx(intfc,i,localrel,errcode); |
| 749 |
if (res == DW_DLV_ERROR) { |
| 750 |
localdoas->ai_object = intfc; |
| 751 |
localdoas->ai_methods = 0; |
| 752 |
_dwarf_destruct_elf_nlaccess(localdoas); |
| 753 |
localdoas = 0; |
| 754 |
return res; |
| 755 |
} |
| 756 |
} |
| 757 |
free(localdoas); |
| 758 |
localdoas = 0; |
| 759 |
return DW_DLV_OK; |
| 760 |
} |
| 761 |
|
| 762 |
static int |
| 763 |
_dwarf_elf_object_access_init( |
| 764 |
int fd, |
| 765 |
unsigned ftype, |
| 766 |
unsigned endian, |
| 767 |
unsigned offsetsize, |
| 768 |
size_t filesize, |
| 769 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 770 |
int *localerrnum) |
| 771 |
{ |
| 772 |
|
| 773 |
int res = 0; |
| 774 |
dwarf_elf_object_access_internals_t *internals = 0; |
| 775 |
Dwarf_Obj_Access_Interface_a *intfc = 0; |
| 776 |
|
| 777 |
internals = malloc(sizeof(dwarf_elf_object_access_internals_t)); |
| 778 |
if (!internals) { |
| 779 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 780 |
/* Impossible case, we hope. Give up. */ |
| 781 |
return DW_DLV_ERROR; |
| 782 |
} |
| 783 |
memset(internals,0,sizeof(*internals)); |
| 784 |
res = _dwarf_elf_object_access_internals_init(internals, |
| 785 |
fd, |
| 786 |
ftype, endian, offsetsize, filesize, |
| 787 |
localerrnum); |
| 788 |
if (res != DW_DLV_OK){ |
| 789 |
return res; |
| 790 |
} |
| 791 |
|
| 792 |
intfc = malloc(sizeof(Dwarf_Obj_Access_Interface_a)); |
| 793 |
if (!intfc) { |
| 794 |
/* Impossible case, we hope. Give up. */ |
| 795 |
free(internals); |
| 796 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 797 |
return DW_DLV_ERROR; |
| 798 |
} |
| 799 |
/* Initialize the interface struct */ |
| 800 |
intfc->ai_object = internals; |
| 801 |
intfc->ai_methods = &elf_nlmethods; |
| 802 |
*binary_interface = intfc; |
| 803 |
return DW_DLV_OK; |
| 804 |
} |