| 1 |
/* Copyright 2018 David Anderson. All rights reserved. |
| 2 |
|
| 3 |
Redistribution and use in source and binary forms, with |
| 4 |
or without modification, are permitted provided that the |
| 5 |
following conditions are met: |
| 6 |
|
| 7 |
Redistributions of source code must retain the above |
| 8 |
copyright notice, this list of conditions and the following |
| 9 |
disclaimer. |
| 10 |
|
| 11 |
Redistributions in binary form must reproduce the above |
| 12 |
copyright notice, this list of conditions and the following |
| 13 |
disclaimer in the documentation and/or other materials |
| 14 |
provided with the distribution. |
| 15 |
|
| 16 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 17 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 18 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 21 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* |
| 32 |
This reads elf headers and creates generic-elf |
| 33 |
structures containing the Elf headers. |
| 34 |
|
| 35 |
These two enums used for type safety in passing |
| 36 |
values. See dwarf_elfread.h |
| 37 |
|
| 38 |
enum RelocRela |
| 39 |
enum RelocOffsetSize |
| 40 |
|
| 41 |
dwarf_elfread.c |
| 42 |
calls |
| 43 |
_dwarf_load_elf_relx(intfc,i,...,enum RelocRela,errcode) |
| 44 |
calls _dwarf_elf_load_a_relx_batch(ep,...enum RelocRela, |
| 45 |
enum RelocOffsetSize,errcode) |
| 46 |
which calls generic_rel_from_rela32(ep,gsh,relp,grel |
| 47 |
or calls generic_rel_from_rela64(ep,gsh,relp,grel |
| 48 |
or calls generic_rel_from_rel32(ep,gsh,relp,grel... |
| 49 |
or calls generic_rel_from_rel64(ep,gsh,relp,grel... |
| 50 |
*/ |
| 51 |
|
| 52 |
#include <config.h> |
| 53 |
|
| 54 |
#include <stddef.h> /* size_t */ |
| 55 |
#include <stdlib.h> /* calloc() free() malloc() */ |
| 56 |
#include <stdio.h> /* printf debugging */ |
| 57 |
#include <string.h> /* memcpy() strcmp() strdup() |
| 58 |
strlen() strncmp() */ |
| 59 |
|
| 60 |
#include "dwarf.h" |
| 61 |
#include "libdwarf.h" |
| 62 |
#include "libdwarf_private.h" |
| 63 |
#include "dwarf_base_types.h" |
| 64 |
#include "dwarf_opaque.h" |
| 65 |
#include "dwarf_memcpy_swap.h" |
| 66 |
#include "dwarf_reading.h" |
| 67 |
#include "dwarf_elf_defines.h" |
| 68 |
#include "dwarf_elfstructs.h" |
| 69 |
#include "dwarf_elfread.h" |
| 70 |
#include "dwarf_object_detector.h" |
| 71 |
#include "dwarf_object_read_common.h" |
| 72 |
#include "dwarf_util.h" |
| 73 |
#include "dwarf_secname_ck.h" |
| 74 |
|
| 75 |
#if 0 /* debugging only dumpsizes() */ |
| 76 |
/* One example of calling this. |
| 77 |
place just before DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE |
| 78 |
dumpsizes(__LINE__,strsectlength,strpsh->gh_offset, |
| 79 |
ep->f_filesize); |
| 80 |
*/ |
| 81 |
static void |
| 82 |
dumpsizes(int line,Dwarf_Unsigned s, |
| 83 |
Dwarf_Unsigned o, |
| 84 |
Dwarf_Unsigned fsz) |
| 85 |
{ |
| 86 |
Dwarf_Unsigned tlen = s + o; |
| 87 |
printf("Size Error DEBUGONLY sz 0x%llx off 0x%llx fsx 0x%llx " |
| 88 |
"sum 0x%llx line %d \n", |
| 89 |
s,o,fsz,tlen,line); |
| 90 |
} |
| 91 |
#endif /*0*/ |
| 92 |
|
| 93 |
int nibblecounts[16] = { |
| 94 |
0,1,1,2, |
| 95 |
1,2,2,3, |
| 96 |
2,2,2,3, |
| 97 |
2,3,3,4 |
| 98 |
}; |
| 99 |
|
| 100 |
static int |
| 101 |
getbitsoncount(Dwarf_Unsigned v_in) |
| 102 |
{ |
| 103 |
int bitscount = 0; |
| 104 |
Dwarf_Unsigned v = v_in; |
| 105 |
|
| 106 |
while (v) { |
| 107 |
unsigned int nibble = v & 0xf; |
| 108 |
bitscount += nibblecounts[nibble]; |
| 109 |
v >>= 4; |
| 110 |
} |
| 111 |
return bitscount; |
| 112 |
} |
| 113 |
|
| 114 |
static int |
| 115 |
_dwarf_load_elf_section_is_dwarf(const char *sname, |
| 116 |
Dwarf_Unsigned sectype, |
| 117 |
int *is_rela,int *is_rel) |
| 118 |
{ |
| 119 |
*is_rel = FALSE; |
| 120 |
*is_rela = FALSE; |
| 121 |
if (_dwarf_ignorethissection(sname)) { |
| 122 |
return FALSE; |
| 123 |
} |
| 124 |
if (sectype == SHT_RELA) { |
| 125 |
*is_rela = TRUE; |
| 126 |
return TRUE; |
| 127 |
} |
| 128 |
if (!strncmp(sname,".rel",4)) { |
| 129 |
if (!strncmp(sname,".rela.",6)) { |
| 130 |
*is_rela = TRUE; |
| 131 |
return TRUE; |
| 132 |
} |
| 133 |
if (!strncmp(sname,".rel.",5)) { |
| 134 |
*is_rela = TRUE; |
| 135 |
return TRUE; |
| 136 |
} |
| 137 |
/* Else something is goofy/Impossible */ |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
if (!strncmp(sname,".debug_",7)) { |
| 141 |
return TRUE; |
| 142 |
} |
| 143 |
if (!strncmp(sname,".zdebug_",8)) { |
| 144 |
return TRUE; |
| 145 |
} |
| 146 |
if (!strcmp(sname,".eh_frame")) { |
| 147 |
return TRUE; |
| 148 |
} |
| 149 |
if (!strncmp(sname,".gdb_index",10)) { |
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
static int |
| 156 |
is_empty_section(Dwarf_Unsigned type) |
| 157 |
{ |
| 158 |
if (type == SHT_NOBITS) { |
| 159 |
return TRUE; |
| 160 |
} |
| 161 |
if (type == SHT_NULL) { |
| 162 |
return TRUE; |
| 163 |
} |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
static int |
| 168 |
generic_ehdr_from_32(dwarf_elf_object_access_internals_t *ep, |
| 169 |
struct generic_ehdr *ehdr, dw_elf32_ehdr *e, |
| 170 |
int *errcode) |
| 171 |
{ |
| 172 |
int i = 0; |
| 173 |
|
| 174 |
for (i = 0; i < EI_NIDENT; ++i) { |
| 175 |
ehdr->ge_ident[i] = e->e_ident[i]; |
| 176 |
} |
| 177 |
ASNAR(ep->f_copy_word,ehdr->ge_type,e->e_type); |
| 178 |
ASNAR(ep->f_copy_word,ehdr->ge_machine,e->e_machine); |
| 179 |
ASNAR(ep->f_copy_word,ehdr->ge_version,e->e_version); |
| 180 |
ASNAR(ep->f_copy_word,ehdr->ge_entry,e->e_entry); |
| 181 |
ASNAR(ep->f_copy_word,ehdr->ge_phoff,e->e_phoff); |
| 182 |
ASNAR(ep->f_copy_word,ehdr->ge_shoff,e->e_shoff); |
| 183 |
ASNAR(ep->f_copy_word,ehdr->ge_flags,e->e_flags); |
| 184 |
ASNAR(ep->f_copy_word,ehdr->ge_ehsize,e->e_ehsize); |
| 185 |
ASNAR(ep->f_copy_word,ehdr->ge_phentsize,e->e_phentsize); |
| 186 |
ASNAR(ep->f_copy_word,ehdr->ge_phnum,e->e_phnum); |
| 187 |
ASNAR(ep->f_copy_word,ehdr->ge_shentsize,e->e_shentsize); |
| 188 |
ASNAR(ep->f_copy_word,ehdr->ge_shnum,e->e_shnum); |
| 189 |
ASNAR(ep->f_copy_word,ehdr->ge_shstrndx,e->e_shstrndx); |
| 190 |
if (!ehdr->ge_shoff) { |
| 191 |
return DW_DLV_NO_ENTRY; |
| 192 |
} |
| 193 |
if (ehdr->ge_shoff < sizeof(dw_elf32_ehdr)) { |
| 194 |
/* offset is inside the header! */ |
| 195 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 196 |
return DW_DLV_ERROR; |
| 197 |
} |
| 198 |
if (ehdr->ge_shstrndx == SHN_XINDEX) { |
| 199 |
ehdr->ge_strndx_extended = TRUE; |
| 200 |
} else { |
| 201 |
ehdr->ge_strndx_in_strndx = TRUE; |
| 202 |
if (ehdr->ge_shstrndx < 1) { |
| 203 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 204 |
return DW_DLV_ERROR; |
| 205 |
} |
| 206 |
} |
| 207 |
/* If !ge_strndx_extended && !ehdr->ge_shnum |
| 208 |
this is a very unusual case. */ |
| 209 |
if (!ehdr->ge_shnum) { |
| 210 |
ehdr->ge_shnum_extended = TRUE; |
| 211 |
} else { |
| 212 |
ehdr->ge_shnum_in_shnum = TRUE; |
| 213 |
if (!ehdr->ge_shnum) { |
| 214 |
return DW_DLV_NO_ENTRY; |
| 215 |
} |
| 216 |
if (ehdr->ge_shnum < 3) { |
| 217 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 218 |
return DW_DLV_ERROR; |
| 219 |
} |
| 220 |
} |
| 221 |
if (ehdr->ge_shnum_in_shnum && |
| 222 |
ehdr->ge_strndx_in_strndx && |
| 223 |
(ehdr->ge_shstrndx >= ehdr->ge_shnum)) { |
| 224 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 225 |
return DW_DLV_ERROR; |
| 226 |
} |
| 227 |
|
| 228 |
ep->f_machine = (unsigned int)ehdr->ge_machine; |
| 229 |
ep->f_ehdr = ehdr; |
| 230 |
ep->f_loc_ehdr.g_name = "Elf File Header"; |
| 231 |
ep->f_loc_ehdr.g_offset = 0; |
| 232 |
ep->f_loc_ehdr.g_count = 1; |
| 233 |
ep->f_loc_ehdr.g_entrysize = sizeof(dw_elf32_ehdr); |
| 234 |
ep->f_loc_ehdr.g_totalsize = sizeof(dw_elf32_ehdr); |
| 235 |
return DW_DLV_OK; |
| 236 |
} |
| 237 |
|
| 238 |
static int |
| 239 |
generic_ehdr_from_64(dwarf_elf_object_access_internals_t* ep, |
| 240 |
struct generic_ehdr *ehdr, dw_elf64_ehdr *e, |
| 241 |
int *errcode) |
| 242 |
{ |
| 243 |
int i = 0; |
| 244 |
|
| 245 |
for (i = 0; i < EI_NIDENT; ++i) { |
| 246 |
ehdr->ge_ident[i] = e->e_ident[i]; |
| 247 |
} |
| 248 |
ASNAR(ep->f_copy_word,ehdr->ge_type,e->e_type); |
| 249 |
ASNAR(ep->f_copy_word,ehdr->ge_machine,e->e_machine); |
| 250 |
ASNAR(ep->f_copy_word,ehdr->ge_version,e->e_version); |
| 251 |
ASNAR(ep->f_copy_word,ehdr->ge_entry,e->e_entry); |
| 252 |
ASNAR(ep->f_copy_word,ehdr->ge_phoff,e->e_phoff); |
| 253 |
ASNAR(ep->f_copy_word,ehdr->ge_shoff,e->e_shoff); |
| 254 |
ASNAR(ep->f_copy_word,ehdr->ge_flags,e->e_flags); |
| 255 |
ASNAR(ep->f_copy_word,ehdr->ge_ehsize,e->e_ehsize); |
| 256 |
ASNAR(ep->f_copy_word,ehdr->ge_phentsize,e->e_phentsize); |
| 257 |
ASNAR(ep->f_copy_word,ehdr->ge_phnum,e->e_phnum); |
| 258 |
ASNAR(ep->f_copy_word,ehdr->ge_shentsize,e->e_shentsize); |
| 259 |
ASNAR(ep->f_copy_word,ehdr->ge_shnum,e->e_shnum); |
| 260 |
ASNAR(ep->f_copy_word,ehdr->ge_shstrndx,e->e_shstrndx); |
| 261 |
if (!ehdr->ge_shoff) { |
| 262 |
return DW_DLV_NO_ENTRY; |
| 263 |
} |
| 264 |
if (ehdr->ge_shoff < sizeof(dw_elf64_ehdr)) { |
| 265 |
/* zero or offset is inside the header! */ |
| 266 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 267 |
return DW_DLV_ERROR; |
| 268 |
} |
| 269 |
if (ehdr->ge_shstrndx == SHN_XINDEX) { |
| 270 |
ehdr->ge_strndx_extended = TRUE; |
| 271 |
} else { |
| 272 |
ehdr->ge_strndx_in_strndx = TRUE; |
| 273 |
if (ehdr->ge_shstrndx < 1) { |
| 274 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 275 |
return DW_DLV_ERROR; |
| 276 |
} |
| 277 |
} |
| 278 |
if (!ehdr->ge_shnum) { |
| 279 |
ehdr->ge_shnum_extended = TRUE; |
| 280 |
} else { |
| 281 |
ehdr->ge_shnum_in_shnum = TRUE; |
| 282 |
if (!ehdr->ge_shnum) { |
| 283 |
return DW_DLV_NO_ENTRY; |
| 284 |
} |
| 285 |
if (ehdr->ge_shnum < 3) { |
| 286 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 287 |
return DW_DLV_ERROR; |
| 288 |
} |
| 289 |
} |
| 290 |
if (ehdr->ge_shnum_in_shnum && |
| 291 |
ehdr->ge_strndx_in_strndx && |
| 292 |
(ehdr->ge_shstrndx >= ehdr->ge_shnum)) { |
| 293 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 294 |
return DW_DLV_ERROR; |
| 295 |
} |
| 296 |
ep->f_machine = (unsigned int)ehdr->ge_machine; |
| 297 |
ep->f_ehdr = ehdr; |
| 298 |
ep->f_loc_ehdr.g_name = "Elf File Header"; |
| 299 |
ep->f_loc_ehdr.g_offset = 0; |
| 300 |
ep->f_loc_ehdr.g_count = 1; |
| 301 |
ep->f_loc_ehdr.g_entrysize = sizeof(dw_elf64_ehdr); |
| 302 |
ep->f_loc_ehdr.g_totalsize = sizeof(dw_elf64_ehdr); |
| 303 |
return DW_DLV_OK; |
| 304 |
} |
| 305 |
|
| 306 |
#if 0 /* ngeneric_phdr_from_phdr32 not needed */ |
| 307 |
static int |
| 308 |
generic_phdr_from_phdr32(dwarf_elf_object_access_internals_t* ep, |
| 309 |
struct generic_phdr **phdr_out, |
| 310 |
Dwarf_Unsigned * count_out, |
| 311 |
Dwarf_Unsigned offset, |
| 312 |
Dwarf_Unsigned entsize, |
| 313 |
Dwarf_Unsigned count, |
| 314 |
int *errcode) |
| 315 |
{ |
| 316 |
dw_elf32_phdr *pph =0; |
| 317 |
dw_elf32_phdr *orig_pph =0; |
| 318 |
struct generic_phdr *gphdr =0; |
| 319 |
struct generic_phdr *orig_gphdr =0; |
| 320 |
Dwarf_Unsigned i = 0; |
| 321 |
int res = 0; |
| 322 |
|
| 323 |
*count_out = 0; |
| 324 |
pph = (dw_elf32_phdr *)calloc(count , entsize); |
| 325 |
if (pph == 0) { |
| 326 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 327 |
return DW_DLV_ERROR; |
| 328 |
} |
| 329 |
gphdr = (struct generic_phdr *)calloc(count,sizeof(*gphdr)); |
| 330 |
if (gphdr == 0) { |
| 331 |
free(pph); |
| 332 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 333 |
return DW_DLV_ERROR; |
| 334 |
} |
| 335 |
|
| 336 |
orig_pph = pph; |
| 337 |
orig_gphdr = gphdr; |
| 338 |
res = RRMOA(ep->f_fd,pph,offset,count*entsize, |
| 339 |
ep->f_filesize,errcode); |
| 340 |
if (res != DW_DLV_OK) { |
| 341 |
free(pph); |
| 342 |
free(gphdr); |
| 343 |
return res; |
| 344 |
} |
| 345 |
for ( i = 0; i < count; |
| 346 |
++i, pph++,gphdr++) { |
| 347 |
ASNAR(ep->f_copy_word,gphdr->gp_type,pph->p_type); |
| 348 |
ASNAR(ep->f_copy_word,gphdr->gp_offset,pph->p_offset); |
| 349 |
ASNAR(ep->f_copy_word,gphdr->gp_vaddr,pph->p_vaddr); |
| 350 |
ASNAR(ep->f_copy_word,gphdr->gp_paddr,pph->p_paddr); |
| 351 |
ASNAR(ep->f_copy_word,gphdr->gp_filesz,pph->p_filesz); |
| 352 |
ASNAR(ep->f_copy_word,gphdr->gp_memsz,pph->p_memsz); |
| 353 |
ASNAR(ep->f_copy_word,gphdr->gp_flags,pph->p_flags); |
| 354 |
ASNAR(ep->f_copy_word,gphdr->gp_align,pph->p_align); |
| 355 |
} |
| 356 |
free(orig_pph); |
| 357 |
*phdr_out = orig_gphdr; |
| 358 |
*count_out = count; |
| 359 |
ep->f_phdr = orig_gphdr; |
| 360 |
ep->f_loc_phdr.g_name = "Program Header"; |
| 361 |
ep->f_loc_phdr.g_offset = offset; |
| 362 |
ep->f_loc_phdr.g_count = count; |
| 363 |
ep->f_loc_phdr.g_entrysize = sizeof(dw_elf32_phdr); |
| 364 |
ep->f_loc_phdr.g_totalsize = sizeof(dw_elf32_phdr)*count; |
| 365 |
return DW_DLV_OK; |
| 366 |
} |
| 367 |
|
| 368 |
static int |
| 369 |
generic_phdr_from_phdr64(dwarf_elf_object_access_internals_t* ep, |
| 370 |
struct generic_phdr **phdr_out, |
| 371 |
Dwarf_Unsigned * count_out, |
| 372 |
Dwarf_Unsigned offset, |
| 373 |
Dwarf_Unsigned entsize, |
| 374 |
Dwarf_Unsigned count, |
| 375 |
int *errcode) |
| 376 |
{ |
| 377 |
dw_elf64_phdr *pph =0; |
| 378 |
dw_elf64_phdr *orig_pph =0; |
| 379 |
struct generic_phdr *gphdr =0; |
| 380 |
struct generic_phdr *orig_gphdr =0; |
| 381 |
int res = 0; |
| 382 |
Dwarf_Unsigned i = 0; |
| 383 |
|
| 384 |
*count_out = 0; |
| 385 |
pph = (dw_elf64_phdr *)calloc(count , entsize); |
| 386 |
if (pph == 0) { |
| 387 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 388 |
return DW_DLV_ERROR; |
| 389 |
} |
| 390 |
gphdr = (struct generic_phdr *)calloc(count,sizeof(*gphdr)); |
| 391 |
if (gphdr == 0) { |
| 392 |
free(pph); |
| 393 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 394 |
return DW_DLV_ERROR; |
| 395 |
} |
| 396 |
|
| 397 |
orig_pph = pph; |
| 398 |
orig_gphdr = gphdr; |
| 399 |
res = RRMOA(ep->f_fd,pph,offset,count*entsize, |
| 400 |
ep->f_filesize,errcode); |
| 401 |
if (res != DW_DLV_OK) { |
| 402 |
free(pph); |
| 403 |
free(gphdr); |
| 404 |
return res; |
| 405 |
} |
| 406 |
for ( i = 0; i < count; |
| 407 |
++i, pph++,gphdr++) { |
| 408 |
ASNAR(ep->f_copy_word,gphdr->gp_type,pph->p_type); |
| 409 |
ASNAR(ep->f_copy_word,gphdr->gp_offset,pph->p_offset); |
| 410 |
ASNAR(ep->f_copy_word,gphdr->gp_vaddr,pph->p_vaddr); |
| 411 |
ASNAR(ep->f_copy_word,gphdr->gp_paddr,pph->p_paddr); |
| 412 |
ASNAR(ep->f_copy_word,gphdr->gp_filesz,pph->p_filesz); |
| 413 |
ASNAR(ep->f_copy_word,gphdr->gp_memsz,pph->p_memsz); |
| 414 |
ASNAR(ep->f_copy_word,gphdr->gp_flags,pph->p_flags); |
| 415 |
ASNAR(ep->f_copy_word,gphdr->gp_align,pph->p_align); |
| 416 |
} |
| 417 |
free(orig_pph); |
| 418 |
*phdr_out = orig_gphdr; |
| 419 |
*count_out = count; |
| 420 |
ep->f_phdr = orig_gphdr; |
| 421 |
ep->f_loc_phdr.g_name = "Program Header"; |
| 422 |
ep->f_loc_phdr.g_offset = offset; |
| 423 |
ep->f_loc_phdr.g_count = count; |
| 424 |
ep->f_loc_phdr.g_entrysize = sizeof(dw_elf64_phdr); |
| 425 |
ep->f_loc_phdr.g_totalsize = sizeof(dw_elf64_phdr)*count; |
| 426 |
return DW_DLV_OK; |
| 427 |
} |
| 428 |
#endif /*0*/ |
| 429 |
|
| 430 |
static void |
| 431 |
copysection32( |
| 432 |
dwarf_elf_object_access_internals_t *ep, |
| 433 |
struct generic_shdr *gshdr, |
| 434 |
dw_elf32_shdr *psh) |
| 435 |
{ |
| 436 |
ASNAR(ep->f_copy_word,gshdr->gh_name,psh->sh_name); |
| 437 |
ASNAR(ep->f_copy_word,gshdr->gh_type,psh->sh_type); |
| 438 |
ASNAR(ep->f_copy_word,gshdr->gh_flags,psh->sh_flags); |
| 439 |
ASNAR(ep->f_copy_word,gshdr->gh_addr,psh->sh_addr); |
| 440 |
ASNAR(ep->f_copy_word,gshdr->gh_offset,psh->sh_offset); |
| 441 |
ASNAR(ep->f_copy_word,gshdr->gh_size,psh->sh_size); |
| 442 |
ASNAR(ep->f_copy_word,gshdr->gh_link,psh->sh_link); |
| 443 |
ASNAR(ep->f_copy_word,gshdr->gh_info,psh->sh_info); |
| 444 |
ASNAR(ep->f_copy_word,gshdr->gh_addralign,psh->sh_addralign); |
| 445 |
ASNAR(ep->f_copy_word,gshdr->gh_entsize,psh->sh_entsize); |
| 446 |
} |
| 447 |
|
| 448 |
static int |
| 449 |
generic_shdr_from_shdr32(dwarf_elf_object_access_internals_t *ep, |
| 450 |
Dwarf_Unsigned * count_out, |
| 451 |
Dwarf_Unsigned offset, |
| 452 |
Dwarf_Unsigned entsize, |
| 453 |
Dwarf_Unsigned count, |
| 454 |
int *errcode) |
| 455 |
{ |
| 456 |
dw_elf32_shdr *psh =0; |
| 457 |
dw_elf32_shdr *orig_psh =0; |
| 458 |
struct generic_ehdr *ehdr = ep->f_ehdr; |
| 459 |
struct generic_shdr *gshdr =0; |
| 460 |
struct generic_shdr *orig_gshdr =0; |
| 461 |
Dwarf_Unsigned i = 0; |
| 462 |
int res = 0; |
| 463 |
|
| 464 |
*count_out = 0; |
| 465 |
psh = (dw_elf32_shdr *)calloc(count , entsize); |
| 466 |
if (!psh) { |
| 467 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 468 |
return DW_DLV_ERROR; |
| 469 |
} |
| 470 |
gshdr = (struct generic_shdr *)calloc(count,sizeof(*gshdr)); |
| 471 |
if (!gshdr) { |
| 472 |
free(psh); |
| 473 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 474 |
return DW_DLV_ERROR; |
| 475 |
} |
| 476 |
|
| 477 |
orig_psh = psh; |
| 478 |
orig_gshdr = gshdr; |
| 479 |
res = RRMOA(ep->f_fd,psh,offset,count*entsize, |
| 480 |
ep->f_filesize,errcode); |
| 481 |
if (res != DW_DLV_OK) { |
| 482 |
free(orig_psh); |
| 483 |
free(orig_gshdr); |
| 484 |
return res; |
| 485 |
} |
| 486 |
for (i = 0; i < count; |
| 487 |
++i, psh++,gshdr++) { |
| 488 |
int isempty = FALSE; |
| 489 |
int bitsoncount = 0; |
| 490 |
|
| 491 |
gshdr->gh_secnum = i; |
| 492 |
copysection32(ep,gshdr,psh); |
| 493 |
#if 1 |
| 494 |
if (gshdr->gh_size >= ep->f_filesize && |
| 495 |
gshdr->gh_type != SHT_NOBITS) { |
| 496 |
free(orig_psh); |
| 497 |
free(orig_gshdr); |
| 498 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 499 |
return DW_DLV_ERROR; |
| 500 |
} |
| 501 |
#endif /* 0 */ |
| 502 |
isempty = is_empty_section(gshdr->gh_type); |
| 503 |
if (i == 0) { |
| 504 |
Dwarf_Unsigned shnum = 0; |
| 505 |
Dwarf_Unsigned shstrx = 0; |
| 506 |
|
| 507 |
/* Catch errors asap */ |
| 508 |
if (!ehdr->ge_shnum_extended) { |
| 509 |
shnum = gshdr->gh_size; |
| 510 |
} |
| 511 |
if (!ehdr->ge_strndx_extended) { |
| 512 |
shstrx = gshdr->gh_link; |
| 513 |
} |
| 514 |
/* We require that section zero be 'empty' |
| 515 |
per the Elf ABI. |
| 516 |
gh_link and gh_size are sometimes used |
| 517 |
with the elf header, so we do not check |
| 518 |
them here. */ |
| 519 |
if (!isempty || gshdr->gh_name || gshdr->gh_flags || |
| 520 |
shnum || shstrx || |
| 521 |
gshdr->gh_addr || |
| 522 |
gshdr->gh_info) { |
| 523 |
free(orig_psh); |
| 524 |
free(orig_gshdr); |
| 525 |
*errcode = DW_DLE_IMPROPER_SECTION_ZERO; |
| 526 |
return DW_DLV_ERROR; |
| 527 |
} |
| 528 |
} |
| 529 |
bitsoncount = getbitsoncount(gshdr->gh_flags); |
| 530 |
if (bitsoncount > 8) { |
| 531 |
free(orig_psh); |
| 532 |
free(orig_gshdr); |
| 533 |
*errcode = DW_DLE_BAD_SECTION_FLAGS; |
| 534 |
return DW_DLV_ERROR; |
| 535 |
} |
| 536 |
|
| 537 |
if (gshdr->gh_type == SHT_REL || gshdr->gh_type == SHT_RELA){ |
| 538 |
gshdr->gh_reloc_target_secnum = gshdr->gh_info; |
| 539 |
} |
| 540 |
} |
| 541 |
free(orig_psh); |
| 542 |
*count_out = count; |
| 543 |
ep->f_shdr = orig_gshdr; |
| 544 |
ep->f_loc_shdr.g_name = "Section Header"; |
| 545 |
ep->f_loc_shdr.g_count = count; |
| 546 |
ep->f_loc_shdr.g_offset = offset; |
| 547 |
ep->f_loc_shdr.g_entrysize = sizeof(dw_elf32_shdr); |
| 548 |
ep->f_loc_shdr.g_totalsize = sizeof(dw_elf32_shdr)*count; |
| 549 |
return DW_DLV_OK; |
| 550 |
} |
| 551 |
|
| 552 |
static void |
| 553 |
copysection64( |
| 554 |
dwarf_elf_object_access_internals_t *ep, |
| 555 |
struct generic_shdr *gshdr, |
| 556 |
dw_elf64_shdr *psh) |
| 557 |
{ |
| 558 |
ASNAR(ep->f_copy_word,gshdr->gh_name,psh->sh_name); |
| 559 |
ASNAR(ep->f_copy_word,gshdr->gh_type,psh->sh_type); |
| 560 |
ASNAR(ep->f_copy_word,gshdr->gh_flags,psh->sh_flags); |
| 561 |
ASNAR(ep->f_copy_word,gshdr->gh_addr,psh->sh_addr); |
| 562 |
ASNAR(ep->f_copy_word,gshdr->gh_offset,psh->sh_offset); |
| 563 |
ASNAR(ep->f_copy_word,gshdr->gh_size,psh->sh_size); |
| 564 |
ASNAR(ep->f_copy_word,gshdr->gh_link,psh->sh_link); |
| 565 |
ASNAR(ep->f_copy_word,gshdr->gh_info,psh->sh_info); |
| 566 |
ASNAR(ep->f_copy_word,gshdr->gh_addralign,psh->sh_addralign); |
| 567 |
ASNAR(ep->f_copy_word,gshdr->gh_entsize,psh->sh_entsize); |
| 568 |
} |
| 569 |
|
| 570 |
static int |
| 571 |
generic_shdr_from_shdr64(dwarf_elf_object_access_internals_t *ep, |
| 572 |
Dwarf_Unsigned * count_out, |
| 573 |
Dwarf_Unsigned offset, |
| 574 |
Dwarf_Unsigned entsize, |
| 575 |
Dwarf_Unsigned count, |
| 576 |
int *errcode) |
| 577 |
{ |
| 578 |
dw_elf64_shdr *psh =0; |
| 579 |
dw_elf64_shdr *orig_psh =0; |
| 580 |
struct generic_shdr *gshdr =0; |
| 581 |
struct generic_shdr *orig_gshdr =0; |
| 582 |
struct generic_ehdr *ehdr = ep->f_ehdr; |
| 583 |
Dwarf_Unsigned i = 0; |
| 584 |
int res = 0; |
| 585 |
|
| 586 |
*count_out = 0; |
| 587 |
psh = (dw_elf64_shdr *)calloc(count , entsize); |
| 588 |
if (!psh) { |
| 589 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 590 |
return DW_DLV_ERROR; |
| 591 |
} |
| 592 |
gshdr = (struct generic_shdr *)calloc(count,sizeof(*gshdr)); |
| 593 |
if (gshdr == 0) { |
| 594 |
free(psh); |
| 595 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 596 |
return DW_DLV_ERROR; |
| 597 |
} |
| 598 |
orig_psh = psh; |
| 599 |
orig_gshdr = gshdr; |
| 600 |
res = RRMOA(ep->f_fd,psh,offset,count*entsize, |
| 601 |
ep->f_filesize,errcode); |
| 602 |
if (res != DW_DLV_OK) { |
| 603 |
free(orig_psh); |
| 604 |
free(orig_gshdr); |
| 605 |
return res; |
| 606 |
} |
| 607 |
for ( i = 0; i < count; |
| 608 |
++i, psh++,gshdr++) { |
| 609 |
int bitsoncount = 0; |
| 610 |
int isempty = FALSE; |
| 611 |
|
| 612 |
gshdr->gh_secnum = i; |
| 613 |
copysection64(ep,gshdr,psh); |
| 614 |
if (gshdr->gh_size >= ep->f_filesize && |
| 615 |
gshdr->gh_type != SHT_NOBITS) { |
| 616 |
free(orig_psh); |
| 617 |
free(orig_gshdr); |
| 618 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 619 |
return DW_DLV_ERROR; |
| 620 |
} |
| 621 |
isempty = is_empty_section(gshdr->gh_type); |
| 622 |
if (i == 0) { |
| 623 |
Dwarf_Unsigned shnum = 0; |
| 624 |
Dwarf_Unsigned shstrx = 0; |
| 625 |
|
| 626 |
/* Catch errors asap */ |
| 627 |
if (!ehdr->ge_shnum_extended) { |
| 628 |
shnum = gshdr->gh_size; |
| 629 |
} |
| 630 |
if (!ehdr->ge_strndx_extended) { |
| 631 |
shstrx = gshdr->gh_link; |
| 632 |
} |
| 633 |
/* We require that section zero be 'empty' |
| 634 |
per the Elf ABI. |
| 635 |
But gh_link and gh_size might be used for |
| 636 |
ge_shstrndx and ge_shnum, respectively*/ |
| 637 |
if (!isempty || gshdr->gh_name || gshdr->gh_flags || |
| 638 |
shnum || shstrx || |
| 639 |
gshdr->gh_addr || |
| 640 |
gshdr->gh_info) { |
| 641 |
free(orig_psh); |
| 642 |
free(orig_gshdr); |
| 643 |
*errcode = DW_DLE_IMPROPER_SECTION_ZERO; |
| 644 |
return DW_DLV_ERROR; |
| 645 |
} |
| 646 |
} |
| 647 |
bitsoncount = getbitsoncount(gshdr->gh_flags); |
| 648 |
if (bitsoncount > 8) { |
| 649 |
free(orig_psh); |
| 650 |
free(orig_gshdr); |
| 651 |
*errcode = DW_DLE_BAD_SECTION_FLAGS; |
| 652 |
return DW_DLV_ERROR; |
| 653 |
} |
| 654 |
|
| 655 |
if (gshdr->gh_type == SHT_REL || |
| 656 |
gshdr->gh_type == SHT_RELA){ |
| 657 |
gshdr->gh_reloc_target_secnum = gshdr->gh_info; |
| 658 |
} |
| 659 |
} |
| 660 |
free(orig_psh); |
| 661 |
*count_out = count; |
| 662 |
ep->f_shdr = orig_gshdr; |
| 663 |
ep->f_loc_shdr.g_name = "Section Header"; |
| 664 |
ep->f_loc_shdr.g_count = count; |
| 665 |
ep->f_loc_shdr.g_offset = offset; |
| 666 |
ep->f_loc_shdr.g_entrysize = sizeof(dw_elf64_shdr); |
| 667 |
ep->f_loc_shdr.g_totalsize = sizeof(dw_elf64_shdr)*count; |
| 668 |
return DW_DLV_OK; |
| 669 |
} |
| 670 |
|
| 671 |
static int |
| 672 |
_dwarf_generic_elf_load_symbols32( |
| 673 |
dwarf_elf_object_access_internals_t *ep, |
| 674 |
struct generic_symentry **gsym_out, |
| 675 |
Dwarf_Unsigned offset,Dwarf_Unsigned size, |
| 676 |
Dwarf_Unsigned *count_out,int *errcode) |
| 677 |
{ |
| 678 |
Dwarf_Unsigned ecount = 0; |
| 679 |
Dwarf_Unsigned size2 = 0; |
| 680 |
Dwarf_Unsigned i = 0; |
| 681 |
dw_elf32_sym *psym = 0; |
| 682 |
dw_elf32_sym *orig_psym = 0; |
| 683 |
struct generic_symentry * gsym = 0; |
| 684 |
struct generic_symentry * orig_gsym = 0; |
| 685 |
int res = 0; |
| 686 |
|
| 687 |
ecount = (long)(size/sizeof(dw_elf32_sym)); |
| 688 |
size2 = ecount * sizeof(dw_elf32_sym); |
| 689 |
if (size != size2) { |
| 690 |
*errcode = DW_DLE_SYMBOL_SECTION_SIZE_ERROR; |
| 691 |
return DW_DLV_ERROR; |
| 692 |
} |
| 693 |
if (size >= ep->f_filesize ) { |
| 694 |
*errcode = DW_DLE_SYMBOL_SECTION_SIZE_ERROR; |
| 695 |
return DW_DLV_ERROR; |
| 696 |
} |
| 697 |
psym = calloc(ecount,sizeof(dw_elf32_sym)); |
| 698 |
if (!psym) { |
| 699 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 700 |
return DW_DLV_ERROR; |
| 701 |
} |
| 702 |
gsym = calloc(ecount,sizeof(struct generic_symentry)); |
| 703 |
if (!gsym) { |
| 704 |
free(psym); |
| 705 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 706 |
return DW_DLV_ERROR; |
| 707 |
} |
| 708 |
res = RRMOA(ep->f_fd,psym,offset,size, |
| 709 |
ep->f_filesize,errcode); |
| 710 |
if (res!= DW_DLV_OK) { |
| 711 |
free(psym); |
| 712 |
free(gsym); |
| 713 |
return res; |
| 714 |
} |
| 715 |
orig_psym = psym; |
| 716 |
orig_gsym = gsym; |
| 717 |
for ( i = 0; i < ecount; ++i,++psym,++gsym) { |
| 718 |
Dwarf_Unsigned bind = 0; |
| 719 |
Dwarf_Unsigned type = 0; |
| 720 |
|
| 721 |
ASNAR(ep->f_copy_word,gsym->gs_name,psym->st_name); |
| 722 |
ASNAR(ep->f_copy_word,gsym->gs_value,psym->st_value); |
| 723 |
ASNAR(ep->f_copy_word,gsym->gs_size,psym->st_size); |
| 724 |
ASNAR(ep->f_copy_word,gsym->gs_info,psym->st_info); |
| 725 |
ASNAR(ep->f_copy_word,gsym->gs_other,psym->st_other); |
| 726 |
ASNAR(ep->f_copy_word,gsym->gs_shndx,psym->st_shndx); |
| 727 |
bind = gsym->gs_info >> 4; |
| 728 |
type = gsym->gs_info & 0xf; |
| 729 |
gsym->gs_bind = bind; |
| 730 |
gsym->gs_type = type; |
| 731 |
} |
| 732 |
*count_out = ecount; |
| 733 |
*gsym_out = orig_gsym; |
| 734 |
free(orig_psym); |
| 735 |
return DW_DLV_OK; |
| 736 |
} |
| 737 |
|
| 738 |
static int |
| 739 |
_dwarf_generic_elf_load_symbols64( |
| 740 |
dwarf_elf_object_access_internals_t *ep, |
| 741 |
struct generic_symentry **gsym_out, |
| 742 |
Dwarf_Unsigned offset,Dwarf_Unsigned size, |
| 743 |
Dwarf_Unsigned *count_out,int *errcode) |
| 744 |
{ |
| 745 |
Dwarf_Unsigned ecount = 0; |
| 746 |
Dwarf_Unsigned size2 = 0; |
| 747 |
Dwarf_Unsigned i = 0; |
| 748 |
dw_elf64_sym *psym = 0; |
| 749 |
dw_elf64_sym *orig_psym = 0; |
| 750 |
struct generic_symentry * gsym = 0; |
| 751 |
struct generic_symentry * orig_gsym = 0; |
| 752 |
int res = 0; |
| 753 |
|
| 754 |
ecount = (long)(size/sizeof(dw_elf64_sym)); |
| 755 |
size2 = ecount * sizeof(dw_elf64_sym); |
| 756 |
if (size != size2) { |
| 757 |
*errcode = DW_DLE_SYMBOL_SECTION_SIZE_ERROR; |
| 758 |
return DW_DLV_ERROR; |
| 759 |
} |
| 760 |
if (size >= ep->f_filesize ) { |
| 761 |
*errcode = DW_DLE_SYMBOL_SECTION_SIZE_ERROR; |
| 762 |
return DW_DLV_ERROR; |
| 763 |
} |
| 764 |
psym = calloc(ecount,sizeof(dw_elf64_sym)); |
| 765 |
if (!psym) { |
| 766 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 767 |
return DW_DLV_ERROR; |
| 768 |
} |
| 769 |
gsym = calloc(ecount,sizeof(struct generic_symentry)); |
| 770 |
if (!gsym) { |
| 771 |
free(psym); |
| 772 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 773 |
return DW_DLV_ERROR; |
| 774 |
} |
| 775 |
res = RRMOA(ep->f_fd,psym,offset,size, |
| 776 |
ep->f_filesize,errcode); |
| 777 |
if (res!= DW_DLV_OK) { |
| 778 |
free(psym); |
| 779 |
free(gsym); |
| 780 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 781 |
return res; |
| 782 |
} |
| 783 |
orig_psym = psym; |
| 784 |
orig_gsym = gsym; |
| 785 |
for ( i = 0; i < ecount; ++i,++psym,++gsym) { |
| 786 |
Dwarf_Unsigned bind = 0; |
| 787 |
Dwarf_Unsigned type = 0; |
| 788 |
|
| 789 |
ASNAR(ep->f_copy_word,gsym->gs_name,psym->st_name); |
| 790 |
ASNAR(ep->f_copy_word,gsym->gs_value,psym->st_value); |
| 791 |
ASNAR(ep->f_copy_word,gsym->gs_size,psym->st_size); |
| 792 |
ASNAR(ep->f_copy_word,gsym->gs_info,psym->st_info); |
| 793 |
ASNAR(ep->f_copy_word,gsym->gs_other,psym->st_other); |
| 794 |
ASNAR(ep->f_copy_word,gsym->gs_shndx,psym->st_shndx); |
| 795 |
bind = gsym->gs_info >> 4; |
| 796 |
type = gsym->gs_info & 0xf; |
| 797 |
gsym->gs_bind = bind; |
| 798 |
gsym->gs_type = type; |
| 799 |
} |
| 800 |
*count_out = ecount; |
| 801 |
*gsym_out = orig_gsym; |
| 802 |
free(orig_psym); |
| 803 |
return DW_DLV_OK; |
| 804 |
} |
| 805 |
|
| 806 |
static int |
| 807 |
_dwarf_generic_elf_load_symbols( |
| 808 |
dwarf_elf_object_access_internals_t *ep, |
| 809 |
Dwarf_Unsigned secnum, |
| 810 |
struct generic_shdr *psh, |
| 811 |
struct generic_symentry **gsym_out, |
| 812 |
Dwarf_Unsigned *count_out,int *errcode) |
| 813 |
{ |
| 814 |
int res = 0; |
| 815 |
struct generic_symentry *gsym = 0; |
| 816 |
Dwarf_Unsigned count = 0; |
| 817 |
|
| 818 |
if (!secnum) { |
| 819 |
return DW_DLV_NO_ENTRY; |
| 820 |
} |
| 821 |
if (psh->gh_size > ep->f_filesize) { |
| 822 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 823 |
return DW_DLV_ERROR; |
| 824 |
} |
| 825 |
if (ep->f_offsetsize == 32) { |
| 826 |
res = _dwarf_generic_elf_load_symbols32(ep, |
| 827 |
&gsym, |
| 828 |
psh->gh_offset,psh->gh_size, |
| 829 |
&count,errcode); |
| 830 |
} else if (ep->f_offsetsize == 64) { |
| 831 |
res = _dwarf_generic_elf_load_symbols64(ep, |
| 832 |
&gsym, |
| 833 |
psh->gh_offset,psh->gh_size, |
| 834 |
&count,errcode); |
| 835 |
} else { |
| 836 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 837 |
return DW_DLV_ERROR; |
| 838 |
} |
| 839 |
if (res == DW_DLV_OK) { |
| 840 |
*gsym_out = gsym; |
| 841 |
*count_out = count; |
| 842 |
} |
| 843 |
return res; |
| 844 |
} |
| 845 |
#if 0 /* dwarf_load_elf_dynsym_symbols() not needed */ |
| 846 |
int |
| 847 |
dwarf_load_elf_dynsym_symbols( |
| 848 |
dwarf_elf_object_access_internals_t *ep, int*errcode) |
| 849 |
{ |
| 850 |
int res = 0; |
| 851 |
struct generic_symentry *gsym = 0; |
| 852 |
Dwarf_Unsigned count = 0; |
| 853 |
Dwarf_Unsigned secnum = ep->f_dynsym_sect_index; |
| 854 |
struct generic_shdr * psh = 0; |
| 855 |
|
| 856 |
if (!secnum) { |
| 857 |
return DW_DLV_NO_ENTRY; |
| 858 |
} |
| 859 |
psh = ep->f_shdr + secnum; |
| 860 |
if we ever use this... gh_size big? |
| 861 |
res = _dwarf_generic_elf_load_symbols(ep, |
| 862 |
secnum, |
| 863 |
psh, |
| 864 |
&gsym, |
| 865 |
&count,errcode); |
| 866 |
if (res == DW_DLV_OK) { |
| 867 |
ep->f_dynsym = gsym; |
| 868 |
ep->f_loc_dynsym.g_count = count; |
| 869 |
} |
| 870 |
return res; |
| 871 |
} |
| 872 |
#endif /*0*/ |
| 873 |
|
| 874 |
int |
| 875 |
_dwarf_load_elf_symtab_symbols( |
| 876 |
dwarf_elf_object_access_internals_t *ep, int*errcode) |
| 877 |
{ |
| 878 |
int res = 0; |
| 879 |
struct generic_symentry *gsym = 0; |
| 880 |
Dwarf_Unsigned count = 0; |
| 881 |
Dwarf_Unsigned secnum = ep->f_symtab_sect_index; |
| 882 |
struct generic_shdr * psh = 0; |
| 883 |
|
| 884 |
if (!secnum) { |
| 885 |
return DW_DLV_NO_ENTRY; |
| 886 |
} |
| 887 |
psh = ep->f_shdr + secnum; |
| 888 |
if (psh->gh_size > ep->f_filesize) { |
| 889 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 890 |
return DW_DLV_ERROR; |
| 891 |
} |
| 892 |
res = _dwarf_generic_elf_load_symbols(ep, |
| 893 |
secnum, |
| 894 |
psh, |
| 895 |
&gsym, |
| 896 |
&count,errcode); |
| 897 |
if (res == DW_DLV_OK) { |
| 898 |
ep->f_symtab = gsym; |
| 899 |
ep->f_loc_symtab.g_count = count; |
| 900 |
} |
| 901 |
return res; |
| 902 |
} |
| 903 |
|
| 904 |
static int |
| 905 |
generic_rel_from_rela32( |
| 906 |
dwarf_elf_object_access_internals_t *ep, |
| 907 |
struct generic_shdr * gsh, |
| 908 |
dw_elf32_rela *relp, |
| 909 |
struct generic_rela *grel, |
| 910 |
int *errcode) |
| 911 |
{ |
| 912 |
Dwarf_Unsigned ecount = 0; |
| 913 |
Dwarf_Unsigned size = gsh->gh_size; |
| 914 |
Dwarf_Unsigned size2 = 0; |
| 915 |
Dwarf_Unsigned i = 0; |
| 916 |
|
| 917 |
ecount = size/sizeof(dw_elf32_rela); |
| 918 |
size2 = ecount * sizeof(dw_elf32_rela); |
| 919 |
if (size >= ep->f_filesize) { |
| 920 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 921 |
return DW_DLV_ERROR; |
| 922 |
} |
| 923 |
if (size != size2) { |
| 924 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 925 |
return DW_DLV_ERROR; |
| 926 |
} |
| 927 |
for ( i = 0; i < ecount; ++i,++relp,++grel) { |
| 928 |
ASNAR(ep->f_copy_word,grel->gr_offset,relp->r_offset); |
| 929 |
ASNAR(ep->f_copy_word,grel->gr_info,relp->r_info); |
| 930 |
/* addend signed */ |
| 931 |
ASNAR(ep->f_copy_word,grel->gr_addend,relp->r_addend); |
| 932 |
SIGN_EXTEND(grel->gr_addend,sizeof(relp->r_addend)); |
| 933 |
grel->gr_sym = grel->gr_info>>8; /* ELF32_R_SYM */ |
| 934 |
grel->gr_type = grel->gr_info & 0xff; |
| 935 |
grel->gr_is_rela = TRUE; |
| 936 |
} |
| 937 |
return DW_DLV_OK; |
| 938 |
} |
| 939 |
|
| 940 |
static int |
| 941 |
generic_rel_from_rela64( |
| 942 |
dwarf_elf_object_access_internals_t *ep, |
| 943 |
struct generic_shdr * gsh, |
| 944 |
dw_elf64_rela *relp, |
| 945 |
struct generic_rela *grel, int *errcode) |
| 946 |
{ |
| 947 |
Dwarf_Unsigned ecount = 0; |
| 948 |
Dwarf_Unsigned size = gsh->gh_size; |
| 949 |
Dwarf_Unsigned size2 = 0; |
| 950 |
Dwarf_Unsigned i = 0; |
| 951 |
int objlittleendian = (ep->f_endian == DW_END_little); |
| 952 |
int ismips64 = (ep->f_machine == EM_MIPS); |
| 953 |
int issparcv9 = (ep->f_machine == EM_SPARCV9); |
| 954 |
|
| 955 |
ecount = size/sizeof(dw_elf64_rela); |
| 956 |
size2 = ecount * sizeof(dw_elf64_rela); |
| 957 |
if (size >= ep->f_filesize) { |
| 958 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 959 |
return DW_DLV_ERROR; |
| 960 |
} |
| 961 |
if (size != size2) { |
| 962 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 963 |
return DW_DLV_ERROR; |
| 964 |
} |
| 965 |
for ( i = 0; i < ecount; ++i,++relp,++grel) { |
| 966 |
ASNAR(ep->f_copy_word,grel->gr_offset,relp->r_offset); |
| 967 |
ASNAR(ep->f_copy_word,grel->gr_info,relp->r_info); |
| 968 |
ASNAR(ep->f_copy_word,grel->gr_addend,relp->r_addend); |
| 969 |
SIGN_EXTEND(grel->gr_addend,sizeof(relp->r_addend)); |
| 970 |
if (ismips64 && objlittleendian ) { |
| 971 |
char realsym[4]; |
| 972 |
|
| 973 |
memcpy(realsym,&relp->r_info,sizeof(realsym)); |
| 974 |
ASNAR(ep->f_copy_word,grel->gr_sym,realsym); |
| 975 |
grel->gr_type = relp->r_info[7]; |
| 976 |
grel->gr_type2 = relp->r_info[6]; |
| 977 |
grel->gr_type3 = relp->r_info[5]; |
| 978 |
} else if (issparcv9) { |
| 979 |
/* Always Big Endian? */ |
| 980 |
char realsym[4]; |
| 981 |
|
| 982 |
memcpy(realsym,&relp->r_info,sizeof(realsym)); |
| 983 |
ASNAR(ep->f_copy_word,grel->gr_sym,realsym); |
| 984 |
grel->gr_type = relp->r_info[7]; |
| 985 |
} else { |
| 986 |
grel->gr_sym = grel->gr_info >> 32; |
| 987 |
grel->gr_type = grel->gr_info & 0xffffffff; |
| 988 |
} |
| 989 |
grel->gr_is_rela = TRUE; |
| 990 |
} |
| 991 |
return DW_DLV_OK; |
| 992 |
} |
| 993 |
|
| 994 |
static int |
| 995 |
generic_rel_from_rel32( |
| 996 |
dwarf_elf_object_access_internals_t *ep, |
| 997 |
struct generic_shdr * gsh, |
| 998 |
dw_elf32_rel *relp, |
| 999 |
struct generic_rela *grel,int *errcode) |
| 1000 |
{ |
| 1001 |
Dwarf_Unsigned ecount = 0; |
| 1002 |
Dwarf_Unsigned size = gsh->gh_size; |
| 1003 |
Dwarf_Unsigned size2 = 0; |
| 1004 |
Dwarf_Unsigned i = 0; |
| 1005 |
|
| 1006 |
ecount = size/sizeof(dw_elf32_rel); |
| 1007 |
size2 = ecount * sizeof(dw_elf32_rel); |
| 1008 |
if (size >= ep->f_filesize) { |
| 1009 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 1010 |
return DW_DLV_ERROR; |
| 1011 |
} |
| 1012 |
if (size != size2) { |
| 1013 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 1014 |
return DW_DLV_ERROR; |
| 1015 |
} |
| 1016 |
for ( i = 0; i < ecount; ++i,++relp,++grel) { |
| 1017 |
ASNAR(ep->f_copy_word,grel->gr_offset,relp->r_offset); |
| 1018 |
ASNAR(ep->f_copy_word,grel->gr_info,relp->r_info); |
| 1019 |
grel->gr_addend = 0; /* Unused for plain .rel */ |
| 1020 |
grel->gr_sym = grel->gr_info >>8; /* ELF32_R_SYM */ |
| 1021 |
grel->gr_is_rela = FALSE; |
| 1022 |
grel->gr_type = grel->gr_info & 0xff; |
| 1023 |
} |
| 1024 |
return DW_DLV_OK; |
| 1025 |
} |
| 1026 |
|
| 1027 |
static int |
| 1028 |
generic_rel_from_rel64( |
| 1029 |
dwarf_elf_object_access_internals_t *ep, |
| 1030 |
struct generic_shdr * gsh, |
| 1031 |
dw_elf64_rel *relp, |
| 1032 |
struct generic_rela *grel,int *errcode) |
| 1033 |
{ |
| 1034 |
Dwarf_Unsigned ecount = 0; |
| 1035 |
Dwarf_Unsigned size = gsh->gh_size; |
| 1036 |
Dwarf_Unsigned size2 = 0; |
| 1037 |
Dwarf_Unsigned i = 0; |
| 1038 |
int objlittleendian = (ep->f_endian == DW_END_little); |
| 1039 |
int ismips64 = (ep->f_machine == EM_MIPS); |
| 1040 |
int issparcv9 = (ep->f_machine == EM_SPARCV9); |
| 1041 |
|
| 1042 |
ecount = size/sizeof(dw_elf64_rel); |
| 1043 |
size2 = ecount * sizeof(dw_elf64_rel); |
| 1044 |
if (size >= ep->f_filesize) { |
| 1045 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 1046 |
return DW_DLV_ERROR; |
| 1047 |
} |
| 1048 |
if (size != size2) { |
| 1049 |
*errcode = DW_DLE_RELOCATION_SECTION_SIZE_ERROR; |
| 1050 |
return DW_DLV_ERROR; |
| 1051 |
} |
| 1052 |
for ( i = 0; i < ecount; ++i,++relp,++grel) { |
| 1053 |
ASNAR(ep->f_copy_word,grel->gr_offset,relp->r_offset); |
| 1054 |
ASNAR(ep->f_copy_word,grel->gr_info,relp->r_info); |
| 1055 |
grel->gr_addend = 0; /* Unused for plain .rel */ |
| 1056 |
if (ismips64 && objlittleendian ) { |
| 1057 |
char realsym[4]; |
| 1058 |
|
| 1059 |
memcpy(realsym,&relp->r_info,sizeof(realsym)); |
| 1060 |
ASNAR(ep->f_copy_word,grel->gr_sym,realsym); |
| 1061 |
grel->gr_type = relp->r_info[7]; |
| 1062 |
grel->gr_type2 = relp->r_info[6]; |
| 1063 |
grel->gr_type3 = relp->r_info[5]; |
| 1064 |
} else if (issparcv9) { |
| 1065 |
/* Always Big Endian? */ |
| 1066 |
char realsym[4]; |
| 1067 |
|
| 1068 |
memcpy(realsym,&relp->r_info,sizeof(realsym)); |
| 1069 |
ASNAR(ep->f_copy_word,grel->gr_sym,realsym); |
| 1070 |
grel->gr_type = relp->r_info[7]; |
| 1071 |
} else { |
| 1072 |
grel->gr_sym = grel->gr_info >>32; |
| 1073 |
grel->gr_type = grel->gr_info & 0xffffffff; |
| 1074 |
} |
| 1075 |
grel->gr_is_rela = FALSE; |
| 1076 |
} |
| 1077 |
return DW_DLV_OK; |
| 1078 |
} |
| 1079 |
|
| 1080 |
#if 0 /* dwarf_load_elf_dynstr() not needed */ |
| 1081 |
int |
| 1082 |
dwarf_load_elf_dynstr( |
| 1083 |
dwarf_elf_object_access_internals_t *ep, int *errcode) |
| 1084 |
{ |
| 1085 |
struct generic_shdr *strpsh = 0; |
| 1086 |
int res = 0; |
| 1087 |
Dwarf_Unsigned strsectindex =0; |
| 1088 |
Dwarf_Unsigned strsectlength = 0; |
| 1089 |
|
| 1090 |
if (!ep->f_dynsym_sect_strings_sect_index) { |
| 1091 |
return DW_DLV_NO_ENTRY; |
| 1092 |
} |
| 1093 |
strsectindex = ep->f_dynsym_sect_strings_sect_index; |
| 1094 |
strsectlength = ep->f_dynsym_sect_strings_max; |
| 1095 |
strpsh = ep->f_shdr + strsectindex; |
| 1096 |
/* Alloc an extra byte as a guaranteed NUL byte |
| 1097 |
at the end of the strings in case the section |
| 1098 |
is corrupted and lacks a NUL at end. */ |
| 1099 |
ep->f_dynsym_sect_strings = calloc(1,strsectlength+1); |
| 1100 |
if (!ep->f_dynsym_sect_strings) { |
| 1101 |
ep->f_dynsym_sect_strings = 0; |
| 1102 |
ep->f_dynsym_sect_strings_max = 0; |
| 1103 |
ep->f_dynsym_sect_strings_sect_index = 0; |
| 1104 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1105 |
return DW_DLV_ERROR; |
| 1106 |
} |
| 1107 |
res = RRMOA(ep->f_fd,ep->f_dynsym_sect_strings, |
| 1108 |
strpsh->gh_offset, |
| 1109 |
strsectlength, |
| 1110 |
ep->f_filesize,errcode); |
| 1111 |
if (res != DW_DLV_OK) { |
| 1112 |
ep->f_dynsym_sect_strings = 0; |
| 1113 |
ep->f_dynsym_sect_strings_max = 0; |
| 1114 |
ep->f_dynsym_sect_strings_sect_index = 0; |
| 1115 |
return res; |
| 1116 |
} |
| 1117 |
return DW_DLV_OK; |
| 1118 |
} |
| 1119 |
#endif /*0*/ |
| 1120 |
|
| 1121 |
int |
| 1122 |
_dwarf_load_elf_symstr( |
| 1123 |
dwarf_elf_object_access_internals_t *ep, int *errcode) |
| 1124 |
{ |
| 1125 |
struct generic_shdr *strpsh = 0; |
| 1126 |
int res = 0; |
| 1127 |
Dwarf_Unsigned strsectindex =0; |
| 1128 |
Dwarf_Unsigned strsectlength = 0; |
| 1129 |
|
| 1130 |
if (!ep->f_symtab_sect_strings_sect_index) { |
| 1131 |
return DW_DLV_NO_ENTRY; |
| 1132 |
} |
| 1133 |
strsectindex = ep->f_symtab_sect_strings_sect_index; |
| 1134 |
strsectlength = ep->f_symtab_sect_strings_max; |
| 1135 |
strpsh = ep->f_shdr + strsectindex; |
| 1136 |
/* Alloc an extra byte as a guaranteed NUL byte |
| 1137 |
at the end of the strings in case the section |
| 1138 |
is corrupted and lacks a NUL at end. */ |
| 1139 |
if (strsectlength > ep->f_filesize || |
| 1140 |
strpsh->gh_offset >ep->f_filesize || |
| 1141 |
(strsectlength + strpsh->gh_offset) > |
| 1142 |
ep->f_filesize) { |
| 1143 |
*errcode = DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE; |
| 1144 |
return DW_DLV_ERROR; |
| 1145 |
} |
| 1146 |
ep->f_symtab_sect_strings = calloc(1,strsectlength+1); |
| 1147 |
if (!ep->f_symtab_sect_strings) { |
| 1148 |
ep->f_symtab_sect_strings = 0; |
| 1149 |
ep->f_symtab_sect_strings_max = 0; |
| 1150 |
ep->f_symtab_sect_strings_sect_index = 0; |
| 1151 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1152 |
return DW_DLV_ERROR; |
| 1153 |
} |
| 1154 |
res = RRMOA(ep->f_fd,ep->f_symtab_sect_strings, |
| 1155 |
strpsh->gh_offset, |
| 1156 |
strsectlength, |
| 1157 |
ep->f_filesize,errcode); |
| 1158 |
if (res != DW_DLV_OK) { |
| 1159 |
free(ep->f_symtab_sect_strings); |
| 1160 |
ep->f_symtab_sect_strings = 0; |
| 1161 |
ep->f_symtab_sect_strings_max = 0; |
| 1162 |
ep->f_symtab_sect_strings_sect_index = 0; |
| 1163 |
return res; |
| 1164 |
} |
| 1165 |
return DW_DLV_OK; |
| 1166 |
} |
| 1167 |
|
| 1168 |
static int |
| 1169 |
_dwarf_elf_load_sectstrings( |
| 1170 |
dwarf_elf_object_access_internals_t *ep, |
| 1171 |
Dwarf_Unsigned stringsection, |
| 1172 |
int *errcode) |
| 1173 |
{ |
| 1174 |
int res = 0; |
| 1175 |
struct generic_shdr *psh = 0; |
| 1176 |
Dwarf_Unsigned secoffset = 0; |
| 1177 |
|
| 1178 |
ep->f_elf_shstrings_length = 0; |
| 1179 |
if (stringsection >= ep->f_ehdr->ge_shnum) { |
| 1180 |
*errcode = DW_DLE_SECTION_INDEX_BAD; |
| 1181 |
return DW_DLV_ERROR; |
| 1182 |
} |
| 1183 |
psh = ep->f_shdr + stringsection; |
| 1184 |
secoffset = psh->gh_offset; |
| 1185 |
if (is_empty_section(psh->gh_type)) { |
| 1186 |
*errcode = DW_DLE_ELF_STRING_SECTION_MISSING; |
| 1187 |
return DW_DLV_ERROR; |
| 1188 |
} |
| 1189 |
if (secoffset >= ep->f_filesize || |
| 1190 |
psh->gh_size > ep->f_filesize || |
| 1191 |
(secoffset + psh->gh_size) > |
| 1192 |
ep->f_filesize) { |
| 1193 |
*errcode = DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE; |
| 1194 |
return DW_DLV_ERROR; |
| 1195 |
} |
| 1196 |
if (psh->gh_size > ep->f_elf_shstrings_max) { |
| 1197 |
free(ep->f_elf_shstrings_data); |
| 1198 |
ep->f_elf_shstrings_data = (char *)malloc(psh->gh_size); |
| 1199 |
ep->f_elf_shstrings_max = psh->gh_size; |
| 1200 |
if (!ep->f_elf_shstrings_data) { |
| 1201 |
ep->f_elf_shstrings_max = 0; |
| 1202 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1203 |
return DW_DLV_ERROR; |
| 1204 |
} |
| 1205 |
} |
| 1206 |
ep->f_elf_shstrings_length = psh->gh_size; |
| 1207 |
res = RRMOA(ep->f_fd,ep->f_elf_shstrings_data,secoffset, |
| 1208 |
psh->gh_size, |
| 1209 |
ep->f_filesize,errcode); |
| 1210 |
return res; |
| 1211 |
} |
| 1212 |
|
| 1213 |
static const dw_elf32_shdr shd32zero; |
| 1214 |
static const struct generic_shdr shdgzero; |
| 1215 |
|
| 1216 |
/* Has a side effect of setting count, number |
| 1217 |
in the ehdr ep points to. */ |
| 1218 |
static int |
| 1219 |
get_counts_from_sec32_zero( |
| 1220 |
dwarf_elf_object_access_internals_t * ep, |
| 1221 |
Dwarf_Unsigned offset, |
| 1222 |
Dwarf_Bool *have_shdr_count, |
| 1223 |
Dwarf_Unsigned *shdr_count, |
| 1224 |
Dwarf_Bool *have_shstrndx_number, |
| 1225 |
Dwarf_Unsigned *shstrndx_number, |
| 1226 |
int *errcode) |
| 1227 |
{ |
| 1228 |
dw_elf32_shdr shd32; |
| 1229 |
struct generic_shdr shdg; |
| 1230 |
int res = 0; |
| 1231 |
Dwarf_Unsigned size = sizeof(shd32); |
| 1232 |
struct generic_ehdr * geh = ep->f_ehdr; |
| 1233 |
|
| 1234 |
shd32 = shd32zero; |
| 1235 |
shdg = shdgzero; |
| 1236 |
res = RRMOA(ep->f_fd,&shd32,offset,size, |
| 1237 |
ep->f_filesize,errcode); |
| 1238 |
if (res != DW_DLV_OK) { |
| 1239 |
return res; |
| 1240 |
} |
| 1241 |
copysection32(ep,&shdg,&shd32); |
| 1242 |
if (geh->ge_shnum_extended) { |
| 1243 |
geh->ge_shnum = shdg.gh_size; |
| 1244 |
geh->ge_shnum_in_shnum = TRUE; |
| 1245 |
if (geh->ge_shnum < 3) { |
| 1246 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 1247 |
return DW_DLV_ERROR; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
*have_shdr_count = TRUE; |
| 1251 |
*shdr_count = geh->ge_shnum; |
| 1252 |
if (geh->ge_strndx_extended) { |
| 1253 |
geh->ge_shstrndx = shdg.gh_link; |
| 1254 |
geh->ge_strndx_in_strndx = TRUE; |
| 1255 |
} |
| 1256 |
if (geh->ge_shnum_in_shnum && |
| 1257 |
geh->ge_strndx_in_strndx&& |
| 1258 |
(geh->ge_shstrndx >= geh->ge_shnum)) { |
| 1259 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 1260 |
return DW_DLV_ERROR; |
| 1261 |
} |
| 1262 |
*have_shstrndx_number = TRUE; |
| 1263 |
*shstrndx_number = geh->ge_shstrndx; |
| 1264 |
return DW_DLV_OK; |
| 1265 |
} |
| 1266 |
|
| 1267 |
static int |
| 1268 |
elf_load_sectheaders32( |
| 1269 |
dwarf_elf_object_access_internals_t *ep, |
| 1270 |
Dwarf_Unsigned offset, |
| 1271 |
Dwarf_Unsigned entsize, |
| 1272 |
Dwarf_Unsigned count, |
| 1273 |
int *errcode) |
| 1274 |
{ |
| 1275 |
Dwarf_Unsigned generic_count = 0; |
| 1276 |
Dwarf_Unsigned shdr_count = 0; |
| 1277 |
Dwarf_Bool have_shdr_count = FALSE; |
| 1278 |
Dwarf_Unsigned shstrndx_number = 0; |
| 1279 |
Dwarf_Bool have_shstrndx_number = FALSE; |
| 1280 |
struct generic_ehdr *ehp = 0; |
| 1281 |
int res = 0; |
| 1282 |
|
| 1283 |
if (entsize < sizeof(dw_elf32_shdr)) { |
| 1284 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1285 |
return DW_DLV_ERROR; |
| 1286 |
} |
| 1287 |
ehp = ep->f_ehdr; |
| 1288 |
if (!ehp->ge_shnum_in_shnum || !ehp->ge_strndx_in_strndx) { |
| 1289 |
res = get_counts_from_sec32_zero(ep,offset, |
| 1290 |
&have_shdr_count,&shdr_count, |
| 1291 |
&have_shstrndx_number,&shstrndx_number, |
| 1292 |
errcode); |
| 1293 |
if (res != DW_DLV_OK) { |
| 1294 |
return res; |
| 1295 |
} |
| 1296 |
if (have_shdr_count) { |
| 1297 |
count = shdr_count; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
if (count == 0) { |
| 1301 |
return DW_DLV_NO_ENTRY; |
| 1302 |
} |
| 1303 |
if ((offset > ep->f_filesize)|| |
| 1304 |
(entsize > 200)|| |
| 1305 |
(count > ep->f_filesize) || |
| 1306 |
((count *entsize +offset) > ep->f_filesize)) { |
| 1307 |
*errcode = DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE; |
| 1308 |
return DW_DLV_ERROR; |
| 1309 |
} |
| 1310 |
res = generic_shdr_from_shdr32(ep,&generic_count, |
| 1311 |
offset,entsize,count,errcode); |
| 1312 |
if (res != DW_DLV_OK) { |
| 1313 |
return res; |
| 1314 |
} |
| 1315 |
if (generic_count != count) { |
| 1316 |
*errcode = DW_DLE_ELF_SECTION_COUNT_MISMATCH; |
| 1317 |
return DW_DLV_ERROR; |
| 1318 |
} |
| 1319 |
return DW_DLV_OK; |
| 1320 |
} |
| 1321 |
static const dw_elf64_shdr shd64zero; |
| 1322 |
/* Has a side effect of setting count, number |
| 1323 |
in the ehdr ep points to. */ |
| 1324 |
static int |
| 1325 |
get_counts_from_sec64_zero( |
| 1326 |
dwarf_elf_object_access_internals_t * ep, |
| 1327 |
Dwarf_Unsigned offset, |
| 1328 |
Dwarf_Bool *have_shdr_count, |
| 1329 |
Dwarf_Unsigned *shdr_count, |
| 1330 |
Dwarf_Bool *have_shstrndx_number, |
| 1331 |
Dwarf_Unsigned *shstrndx_number, |
| 1332 |
int *errcode) |
| 1333 |
{ |
| 1334 |
dw_elf64_shdr shd64; |
| 1335 |
struct generic_shdr shdg; |
| 1336 |
int res = 0; |
| 1337 |
Dwarf_Unsigned size = sizeof(shd64); |
| 1338 |
struct generic_ehdr * geh = ep->f_ehdr; |
| 1339 |
|
| 1340 |
shd64 = shd64zero; |
| 1341 |
shdg = shdgzero; |
| 1342 |
res = RRMOA(ep->f_fd,&shd64,offset,size, |
| 1343 |
ep->f_filesize,errcode); |
| 1344 |
if (res != DW_DLV_OK) { |
| 1345 |
return res; |
| 1346 |
} |
| 1347 |
copysection64(ep,&shdg,&shd64); |
| 1348 |
if (geh->ge_shnum_extended) { |
| 1349 |
geh->ge_shnum = shdg.gh_size; |
| 1350 |
geh->ge_shnum_in_shnum = TRUE; |
| 1351 |
if (geh->ge_shnum < 3) { |
| 1352 |
*errcode = DW_DLE_TOO_FEW_SECTIONS; |
| 1353 |
return DW_DLV_ERROR; |
| 1354 |
} |
| 1355 |
} |
| 1356 |
*have_shdr_count = TRUE; |
| 1357 |
*shdr_count = geh->ge_shnum; |
| 1358 |
if (geh->ge_strndx_extended) { |
| 1359 |
geh->ge_shstrndx = shdg.gh_link; |
| 1360 |
geh->ge_strndx_in_strndx = TRUE; |
| 1361 |
} |
| 1362 |
if (geh->ge_shnum_in_shnum && |
| 1363 |
geh->ge_strndx_in_strndx && |
| 1364 |
(geh->ge_shstrndx >= geh->ge_shnum)) { |
| 1365 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 1366 |
return DW_DLV_ERROR; |
| 1367 |
} |
| 1368 |
|
| 1369 |
*have_shstrndx_number = TRUE; |
| 1370 |
*shstrndx_number = geh->ge_shstrndx; |
| 1371 |
return DW_DLV_OK; |
| 1372 |
} |
| 1373 |
|
| 1374 |
static int |
| 1375 |
elf_load_sectheaders64( |
| 1376 |
dwarf_elf_object_access_internals_t *ep, |
| 1377 |
Dwarf_Unsigned offset,Dwarf_Unsigned entsize, |
| 1378 |
Dwarf_Unsigned count,int*errcode) |
| 1379 |
{ |
| 1380 |
Dwarf_Unsigned generic_count = 0; |
| 1381 |
Dwarf_Unsigned shdr_count = 0; |
| 1382 |
Dwarf_Bool have_shdr_count = FALSE; |
| 1383 |
Dwarf_Unsigned shstrndx_number = 0; |
| 1384 |
Dwarf_Bool have_shstrndx_number = FALSE; |
| 1385 |
struct generic_ehdr *ehp = 0; |
| 1386 |
int res = 0; |
| 1387 |
|
| 1388 |
ehp = ep->f_ehdr; |
| 1389 |
if (!ehp->ge_shnum_in_shnum || !ehp->ge_strndx_in_strndx ) { |
| 1390 |
res = get_counts_from_sec64_zero(ep,offset, |
| 1391 |
&have_shdr_count,&shdr_count, |
| 1392 |
&have_shstrndx_number,&shstrndx_number, |
| 1393 |
errcode); |
| 1394 |
if (res != DW_DLV_OK) { |
| 1395 |
return res; |
| 1396 |
} |
| 1397 |
if (have_shdr_count) { |
| 1398 |
count = shdr_count; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
if (count == 0) { |
| 1402 |
return DW_DLV_NO_ENTRY; |
| 1403 |
} |
| 1404 |
if (entsize < sizeof(dw_elf64_shdr)) { |
| 1405 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1406 |
return DW_DLV_ERROR; |
| 1407 |
} |
| 1408 |
if ((offset > ep->f_filesize)|| |
| 1409 |
(entsize > 200)|| |
| 1410 |
(count > ep->f_filesize) || |
| 1411 |
((count *entsize +offset) > ep->f_filesize)) { |
| 1412 |
*errcode = DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE; |
| 1413 |
return DW_DLV_ERROR; |
| 1414 |
} |
| 1415 |
|
| 1416 |
res = generic_shdr_from_shdr64(ep,&generic_count, |
| 1417 |
offset,entsize,count,errcode); |
| 1418 |
if (res != DW_DLV_OK) { |
| 1419 |
return res; |
| 1420 |
} |
| 1421 |
if (generic_count != count) { |
| 1422 |
*errcode = DW_DLE_ELF_SECTION_COUNT_MISMATCH; |
| 1423 |
return DW_DLV_ERROR; |
| 1424 |
} |
| 1425 |
return DW_DLV_OK; |
| 1426 |
} |
| 1427 |
|
| 1428 |
static int |
| 1429 |
_dwarf_elf_load_a_relx_batch( |
| 1430 |
dwarf_elf_object_access_internals_t *ep, |
| 1431 |
struct generic_shdr * gsh, |
| 1432 |
struct generic_rela ** grel_out, |
| 1433 |
Dwarf_Unsigned *count_out, |
| 1434 |
enum RelocRela localrela, |
| 1435 |
enum RelocOffsetSize localoffsize, |
| 1436 |
int *errcode) |
| 1437 |
{ |
| 1438 |
Dwarf_Unsigned count = 0; |
| 1439 |
Dwarf_Unsigned size = 0; |
| 1440 |
Dwarf_Unsigned size2 = 0; |
| 1441 |
Dwarf_Unsigned sizeg = 0; |
| 1442 |
Dwarf_Unsigned offset = 0; |
| 1443 |
int res = 0; |
| 1444 |
Dwarf_Unsigned object_reclen = 0; |
| 1445 |
struct generic_rela *grel = 0; |
| 1446 |
|
| 1447 |
/* ASSERT: Caller guarantees localoffsetsize |
| 1448 |
is a valid 4 or 8. */ |
| 1449 |
/* ASSERT: Caller guarantees localrela is one |
| 1450 |
of the 2 valid values 1 or 2 */ |
| 1451 |
|
| 1452 |
offset = gsh->gh_offset; |
| 1453 |
size = gsh->gh_size; |
| 1454 |
if (size == 0) { |
| 1455 |
return DW_DLV_NO_ENTRY; |
| 1456 |
} |
| 1457 |
if ((offset > ep->f_filesize)|| |
| 1458 |
(size > ep->f_filesize) || |
| 1459 |
((size +offset) > ep->f_filesize)) { |
| 1460 |
*errcode = DW_DLE_SECTION_SIZE_OR_OFFSET_LARGE; |
| 1461 |
return DW_DLV_ERROR; |
| 1462 |
} |
| 1463 |
if (localoffsize == RelocOffset32) { |
| 1464 |
if (localrela == RelocIsRela) { |
| 1465 |
object_reclen = sizeof(dw_elf32_rela); |
| 1466 |
count = (long)(size/object_reclen); |
| 1467 |
size2 = count * object_reclen; |
| 1468 |
if (size != size2) { |
| 1469 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1470 |
return DW_DLV_ERROR; |
| 1471 |
} |
| 1472 |
} else { |
| 1473 |
object_reclen = sizeof(dw_elf32_rel); |
| 1474 |
count = (long)(size/object_reclen); |
| 1475 |
size2 = count * object_reclen; |
| 1476 |
if (size != size2) { |
| 1477 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1478 |
return DW_DLV_ERROR; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} else { |
| 1482 |
if (localrela == RelocIsRela) { |
| 1483 |
object_reclen = sizeof(dw_elf64_rela); |
| 1484 |
count = (long)(size/object_reclen); |
| 1485 |
size2 = count * object_reclen; |
| 1486 |
if (size != size2) { |
| 1487 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1488 |
return DW_DLV_ERROR; |
| 1489 |
} |
| 1490 |
} else { |
| 1491 |
object_reclen = sizeof(dw_elf64_rel); |
| 1492 |
count = (long)(size/object_reclen); |
| 1493 |
size2 = count * object_reclen; |
| 1494 |
if (size != size2) { |
| 1495 |
*errcode = DW_DLE_SECTION_SIZE_ERROR; |
| 1496 |
return DW_DLV_ERROR; |
| 1497 |
} |
| 1498 |
} |
| 1499 |
} |
| 1500 |
sizeg = count*sizeof(struct generic_rela); |
| 1501 |
grel = (struct generic_rela *)malloc(sizeg); |
| 1502 |
if (!grel) { |
| 1503 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1504 |
return DW_DLV_ERROR; |
| 1505 |
} |
| 1506 |
if (localoffsize == RelocOffset32) { |
| 1507 |
if (localrela == RelocIsRela) { |
| 1508 |
dw_elf32_rela *relp = 0; |
| 1509 |
relp = (dw_elf32_rela *)malloc(size); |
| 1510 |
if (!relp) { |
| 1511 |
free(grel); |
| 1512 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1513 |
return DW_DLV_ERROR; |
| 1514 |
} |
| 1515 |
res = RRMOA(ep->f_fd,relp,offset,size, |
| 1516 |
ep->f_filesize,errcode); |
| 1517 |
if (res != DW_DLV_OK) { |
| 1518 |
free(relp); |
| 1519 |
free(grel); |
| 1520 |
return res; |
| 1521 |
} |
| 1522 |
res = generic_rel_from_rela32(ep,gsh,relp,grel,errcode); |
| 1523 |
free(relp); |
| 1524 |
} else { |
| 1525 |
dw_elf32_rel *relp = 0; |
| 1526 |
relp = (dw_elf32_rel *)malloc(size); |
| 1527 |
if (!relp) { |
| 1528 |
free(grel); |
| 1529 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1530 |
return DW_DLV_ERROR; |
| 1531 |
} |
| 1532 |
res = RRMOA(ep->f_fd,relp,offset,size, |
| 1533 |
ep->f_filesize,errcode); |
| 1534 |
if (res != DW_DLV_OK) { |
| 1535 |
free(relp); |
| 1536 |
free(grel); |
| 1537 |
return res; |
| 1538 |
} |
| 1539 |
res = generic_rel_from_rel32(ep,gsh,relp,grel,errcode); |
| 1540 |
free(relp); |
| 1541 |
} |
| 1542 |
} else { |
| 1543 |
if (localrela == RelocIsRela) { |
| 1544 |
dw_elf64_rela *relp = 0; |
| 1545 |
relp = (dw_elf64_rela *)malloc(size); |
| 1546 |
if (!relp) { |
| 1547 |
free(grel); |
| 1548 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1549 |
return DW_DLV_ERROR; |
| 1550 |
} |
| 1551 |
res = RRMOA(ep->f_fd,relp,offset,size, |
| 1552 |
ep->f_filesize,errcode); |
| 1553 |
if (res != DW_DLV_OK) { |
| 1554 |
free(relp); |
| 1555 |
free(grel); |
| 1556 |
return res; |
| 1557 |
} |
| 1558 |
res = generic_rel_from_rela64(ep,gsh,relp,grel,errcode); |
| 1559 |
free(relp); |
| 1560 |
} else { |
| 1561 |
dw_elf64_rel *relp = 0; |
| 1562 |
relp = (dw_elf64_rel *)malloc(size); |
| 1563 |
if (!relp) { |
| 1564 |
free(grel); |
| 1565 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1566 |
return DW_DLV_ERROR; |
| 1567 |
} |
| 1568 |
res = RRMOA(ep->f_fd,relp,offset,size, |
| 1569 |
ep->f_filesize,errcode); |
| 1570 |
if (res != DW_DLV_OK) { |
| 1571 |
free(relp); |
| 1572 |
free(grel); |
| 1573 |
return res; |
| 1574 |
} |
| 1575 |
res = generic_rel_from_rel64(ep,gsh,relp,grel,errcode); |
| 1576 |
free(relp); |
| 1577 |
} |
| 1578 |
} |
| 1579 |
if (res == DW_DLV_OK) { |
| 1580 |
gsh->gh_relcount = count; |
| 1581 |
gsh->gh_rels = grel; |
| 1582 |
*count_out = count; |
| 1583 |
*grel_out = grel; |
| 1584 |
return res; |
| 1585 |
} |
| 1586 |
/* Some sort of issue */ |
| 1587 |
count_out = 0; |
| 1588 |
free(grel); |
| 1589 |
return res; |
| 1590 |
} |
| 1591 |
|
| 1592 |
/* Is this rel/rela section related to dwarf at all? |
| 1593 |
set oksecnum zero if not. Else set targ secnum. |
| 1594 |
Never returns DW_DLV_NO_ENTRY. */ |
| 1595 |
static int |
| 1596 |
this_rel_is_a_section_dwarf_related( |
| 1597 |
dwarf_elf_object_access_internals_t *ep, |
| 1598 |
struct generic_shdr *gshdr, |
| 1599 |
unsigned *oksecnum_out, |
| 1600 |
int *errcode) |
| 1601 |
{ |
| 1602 |
Dwarf_Unsigned oksecnum = 0; |
| 1603 |
struct generic_shdr *gstarg = 0; |
| 1604 |
|
| 1605 |
if (gshdr->gh_type != SHT_RELA && |
| 1606 |
gshdr->gh_type != SHT_REL) { |
| 1607 |
*oksecnum_out = 0; |
| 1608 |
return DW_DLV_OK; |
| 1609 |
} |
| 1610 |
oksecnum = gshdr->gh_reloc_target_secnum; |
| 1611 |
if (oksecnum >= ep->f_loc_shdr.g_count) { |
| 1612 |
*oksecnum_out = 0; |
| 1613 |
*errcode = DW_DLE_ELF_SECTION_ERROR; |
| 1614 |
return DW_DLV_ERROR; |
| 1615 |
} |
| 1616 |
gstarg = ep->f_shdr+oksecnum; |
| 1617 |
if (!gstarg->gh_is_dwarf) { |
| 1618 |
*oksecnum_out = 0; /* no reloc needed. */ |
| 1619 |
return DW_DLV_OK; |
| 1620 |
} |
| 1621 |
*oksecnum_out = (unsigned)oksecnum; |
| 1622 |
return DW_DLV_OK; |
| 1623 |
} |
| 1624 |
/* Secnum here is the secnum of rela. Not |
| 1625 |
the target of the relocations. |
| 1626 |
This also loads .rel. */ |
| 1627 |
int |
| 1628 |
_dwarf_load_elf_relx( |
| 1629 |
dwarf_elf_object_access_internals_t *ep, |
| 1630 |
Dwarf_Unsigned secnum, |
| 1631 |
enum RelocRela localr, |
| 1632 |
int *errcode) |
| 1633 |
{ |
| 1634 |
struct generic_shdr *gshdr = 0; |
| 1635 |
Dwarf_Unsigned seccount = 0; |
| 1636 |
unsigned offsetsize = 0; |
| 1637 |
struct generic_rela *grp = 0; |
| 1638 |
Dwarf_Unsigned count_read = 0; |
| 1639 |
int res = 0; |
| 1640 |
unsigned oksec = 0; |
| 1641 |
enum RelocOffsetSize localoffsize = RelocOffset32; |
| 1642 |
|
| 1643 |
/* ASSERT: Caller guarantees localr is |
| 1644 |
a valid RelocRela */ |
| 1645 |
if (!ep) { |
| 1646 |
*errcode = DW_DLE_INTERNAL_NULL_POINTER; |
| 1647 |
return DW_DLV_ERROR; |
| 1648 |
} |
| 1649 |
offsetsize = ep->f_offsetsize; |
| 1650 |
seccount = ep->f_loc_shdr.g_count; |
| 1651 |
if (secnum >= seccount) { |
| 1652 |
*errcode = DW_DLE_ELF_SECTION_ERROR; |
| 1653 |
return DW_DLV_ERROR; |
| 1654 |
} |
| 1655 |
gshdr = ep->f_shdr +secnum; |
| 1656 |
if (is_empty_section(gshdr->gh_type)) { |
| 1657 |
|
| 1658 |
return DW_DLV_NO_ENTRY; |
| 1659 |
} |
| 1660 |
|
| 1661 |
res = this_rel_is_a_section_dwarf_related(ep,gshdr, |
| 1662 |
&oksec,errcode); |
| 1663 |
if (res == DW_DLV_ERROR) { |
| 1664 |
return res; |
| 1665 |
} |
| 1666 |
if (!oksec) { |
| 1667 |
return DW_DLV_OK; |
| 1668 |
} |
| 1669 |
/* We will actually read these relocations. */ |
| 1670 |
if (offsetsize == 64) { |
| 1671 |
localoffsize = RelocOffset64; |
| 1672 |
} else if (offsetsize == 32) { |
| 1673 |
localoffsize = RelocOffset32; |
| 1674 |
} else { |
| 1675 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 1676 |
return DW_DLV_ERROR; |
| 1677 |
} |
| 1678 |
/* ASSERT: localoffsize is now a valid enum value, |
| 1679 |
one of the two defined. */ |
| 1680 |
res = _dwarf_elf_load_a_relx_batch(ep, |
| 1681 |
gshdr,&grp,&count_read,localr,localoffsize,errcode); |
| 1682 |
if (res == DW_DLV_ERROR) { |
| 1683 |
return res; |
| 1684 |
} |
| 1685 |
if (res == DW_DLV_NO_ENTRY) { |
| 1686 |
return res; |
| 1687 |
} |
| 1688 |
gshdr->gh_rels = grp; |
| 1689 |
gshdr->gh_relcount = count_read; |
| 1690 |
return DW_DLV_OK; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static int |
| 1694 |
validate_section_name_string(Dwarf_Unsigned section_length, |
| 1695 |
Dwarf_Unsigned string_loc_index, |
| 1696 |
const char * strings_start, |
| 1697 |
int * errcode) |
| 1698 |
{ |
| 1699 |
const char *endpoint = strings_start + section_length; |
| 1700 |
const char *cur = 0; |
| 1701 |
|
| 1702 |
if (section_length <= string_loc_index) { |
| 1703 |
*errcode = DW_DLE_SECTION_STRING_OFFSET_BAD; |
| 1704 |
return DW_DLV_ERROR; |
| 1705 |
} |
| 1706 |
cur = string_loc_index+strings_start; |
| 1707 |
for ( ; cur < endpoint;++cur) { |
| 1708 |
if (!*cur) { |
| 1709 |
return DW_DLV_OK; |
| 1710 |
} |
| 1711 |
} |
| 1712 |
*errcode = DW_DLE_SECTION_STRING_OFFSET_BAD; |
| 1713 |
return DW_DLV_ERROR; |
| 1714 |
} |
| 1715 |
|
| 1716 |
/* Without proper section names in place nothing |
| 1717 |
is going to work in reading DWARF sections. */ |
| 1718 |
static int |
| 1719 |
_dwarf_elf_load_sect_namestring( |
| 1720 |
dwarf_elf_object_access_internals_t *ep, |
| 1721 |
int *errcode) |
| 1722 |
{ |
| 1723 |
struct generic_shdr *gshdr = 0; |
| 1724 |
Dwarf_Unsigned generic_count = 0; |
| 1725 |
Dwarf_Unsigned i = 1; |
| 1726 |
const char *stringsecbase = 0; |
| 1727 |
|
| 1728 |
stringsecbase = ep->f_elf_shstrings_data; |
| 1729 |
gshdr = ep->f_shdr; |
| 1730 |
generic_count = ep->f_loc_shdr.g_count; |
| 1731 |
for (i = 0; i < generic_count; i++, ++gshdr) { |
| 1732 |
const char *namestr = |
| 1733 |
"<No valid Elf section strings exist>"; |
| 1734 |
int res = 0; |
| 1735 |
|
| 1736 |
if (!ep->f_ehdr->ge_shstrndx || !stringsecbase) { |
| 1737 |
gshdr->gh_namestring = namestr; |
| 1738 |
continue; |
| 1739 |
} |
| 1740 |
namestr = "<Invalid sh_name value. Corrupt Elf.>"; |
| 1741 |
res = validate_section_name_string(ep->f_elf_shstrings_length, |
| 1742 |
gshdr->gh_name, stringsecbase, |
| 1743 |
errcode); |
| 1744 |
if (res != DW_DLV_OK) { |
| 1745 |
gshdr->gh_namestring = namestr; |
| 1746 |
if (res == DW_DLV_ERROR) { |
| 1747 |
return res; |
| 1748 |
} |
| 1749 |
/* no entry, missing strings. */ |
| 1750 |
*errcode = DW_DLE_NO_SECT_STRINGS; |
| 1751 |
return DW_DLV_ERROR; |
| 1752 |
} else { |
| 1753 |
gshdr->gh_namestring = stringsecbase + gshdr->gh_name; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
return DW_DLV_OK; |
| 1757 |
} |
| 1758 |
|
| 1759 |
/* The C standard ensures these are all appropriate |
| 1760 |
zero bits. */ |
| 1761 |
static const dw_elf32_ehdr eh32_zero; |
| 1762 |
static const dw_elf64_ehdr eh64_zero; |
| 1763 |
|
| 1764 |
static int |
| 1765 |
elf_load_elf_header32( |
| 1766 |
dwarf_elf_object_access_internals_t *ep,int *errcode) |
| 1767 |
{ |
| 1768 |
int res = 0; |
| 1769 |
dw_elf32_ehdr ehdr32; |
| 1770 |
struct generic_ehdr *ehdr = 0; |
| 1771 |
|
| 1772 |
ehdr32 = eh32_zero; |
| 1773 |
res = RRMOA(ep->f_fd,&ehdr32,0,sizeof(ehdr32), |
| 1774 |
ep->f_filesize,errcode); |
| 1775 |
if (res != DW_DLV_OK) { |
| 1776 |
return res; |
| 1777 |
} |
| 1778 |
ehdr = (struct generic_ehdr *)calloc(1, |
| 1779 |
sizeof(struct generic_ehdr)); |
| 1780 |
if (!ehdr) { |
| 1781 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1782 |
return DW_DLV_ERROR; |
| 1783 |
} |
| 1784 |
res = generic_ehdr_from_32(ep,ehdr,&ehdr32,errcode); |
| 1785 |
if (res != DW_DLV_OK) { |
| 1786 |
free(ehdr); |
| 1787 |
return res; |
| 1788 |
} |
| 1789 |
ep->f_machine = (unsigned)ehdr->ge_machine; |
| 1790 |
ep->f_flags = ehdr->ge_flags; |
| 1791 |
return res; |
| 1792 |
} |
| 1793 |
static int |
| 1794 |
elf_load_elf_header64( |
| 1795 |
dwarf_elf_object_access_internals_t *ep,int *errcode) |
| 1796 |
{ |
| 1797 |
int res = 0; |
| 1798 |
dw_elf64_ehdr ehdr64; |
| 1799 |
struct generic_ehdr *ehdr = 0; |
| 1800 |
|
| 1801 |
ehdr64 = eh64_zero; |
| 1802 |
res = RRMOA(ep->f_fd,&ehdr64,0,sizeof(ehdr64), |
| 1803 |
ep->f_filesize,errcode); |
| 1804 |
if (res != DW_DLV_OK) { |
| 1805 |
return res; |
| 1806 |
} |
| 1807 |
ehdr = (struct generic_ehdr *)calloc(1, |
| 1808 |
sizeof(struct generic_ehdr)); |
| 1809 |
if (!ehdr) { |
| 1810 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1811 |
return DW_DLV_ERROR; |
| 1812 |
} |
| 1813 |
res = generic_ehdr_from_64(ep,ehdr,&ehdr64,errcode); |
| 1814 |
if (res != DW_DLV_OK) { |
| 1815 |
free(ehdr); |
| 1816 |
return res; |
| 1817 |
} |
| 1818 |
ep->f_machine = (unsigned)ehdr->ge_machine; |
| 1819 |
ep->f_flags = ehdr->ge_flags; |
| 1820 |
return res; |
| 1821 |
} |
| 1822 |
|
| 1823 |
int |
| 1824 |
_dwarf_load_elf_header( |
| 1825 |
dwarf_elf_object_access_internals_t *ep,int*errcode) |
| 1826 |
{ |
| 1827 |
unsigned offsetsize = ep->f_offsetsize; |
| 1828 |
int res = 0; |
| 1829 |
|
| 1830 |
if (offsetsize == 32) { |
| 1831 |
res = elf_load_elf_header32(ep,errcode); |
| 1832 |
} else if (offsetsize == 64) { |
| 1833 |
if (sizeof(Dwarf_Unsigned) < 8) { |
| 1834 |
*errcode = DW_DLE_INTEGER_TOO_SMALL; |
| 1835 |
return DW_DLV_ERROR; |
| 1836 |
} |
| 1837 |
res = elf_load_elf_header64(ep,errcode); |
| 1838 |
} else { |
| 1839 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 1840 |
return DW_DLV_ERROR; |
| 1841 |
} |
| 1842 |
return res; |
| 1843 |
} |
| 1844 |
|
| 1845 |
static int |
| 1846 |
validate_links( |
| 1847 |
dwarf_elf_object_access_internals_t *ep, |
| 1848 |
Dwarf_Unsigned knownsect, |
| 1849 |
Dwarf_Unsigned string_sect, |
| 1850 |
int *errcode) |
| 1851 |
{ |
| 1852 |
struct generic_shdr* pshk = 0; |
| 1853 |
|
| 1854 |
if (!knownsect) { |
| 1855 |
return DW_DLV_OK; |
| 1856 |
} |
| 1857 |
if (!string_sect) { |
| 1858 |
*errcode = DW_DLE_ELF_STRING_SECTION_ERROR; |
| 1859 |
return DW_DLV_ERROR; |
| 1860 |
} |
| 1861 |
pshk = ep->f_shdr + knownsect; |
| 1862 |
if (string_sect != pshk->gh_link) { |
| 1863 |
*errcode = DW_DLE_ELF_SECTION_LINK_ERROR; |
| 1864 |
return DW_DLV_ERROR; |
| 1865 |
} |
| 1866 |
return DW_DLV_OK; |
| 1867 |
} |
| 1868 |
|
| 1869 |
static int |
| 1870 |
string_endswith(const char *n,const char *q) |
| 1871 |
{ |
| 1872 |
size_t len = strlen(n); |
| 1873 |
size_t qlen = strlen(q); |
| 1874 |
const char *startpt = 0; |
| 1875 |
|
| 1876 |
if ( len < qlen) { |
| 1877 |
return FALSE; |
| 1878 |
} |
| 1879 |
startpt = n + (len-qlen); |
| 1880 |
if (strcmp(startpt,q)) { |
| 1881 |
return FALSE; |
| 1882 |
} |
| 1883 |
return TRUE; |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* We are allowing either SHT_GROUP or .group to indicate |
| 1887 |
a group section, but really one should have both |
| 1888 |
or neither! */ |
| 1889 |
static int |
| 1890 |
elf_sht_groupsec(Dwarf_Unsigned type, const char *sname) |
| 1891 |
{ |
| 1892 |
/* ARM compilers name SHT group "__ARM_grp<long name here>" |
| 1893 |
not .group */ |
| 1894 |
if ((type == SHT_GROUP) || (!strcmp(sname,".group"))){ |
| 1895 |
return TRUE; |
| 1896 |
} |
| 1897 |
return FALSE; |
| 1898 |
} |
| 1899 |
|
| 1900 |
static int |
| 1901 |
elf_flagmatches(Dwarf_Unsigned flagsword,Dwarf_Unsigned flag) |
| 1902 |
{ |
| 1903 |
if ((flagsword&flag) == flag) { |
| 1904 |
return TRUE; |
| 1905 |
} |
| 1906 |
return FALSE; |
| 1907 |
} |
| 1908 |
|
| 1909 |
/* For SHT_GROUP sections. |
| 1910 |
A group section starts with a 32bit flag |
| 1911 |
word with value 1. |
| 1912 |
32bit section numbers of the sections |
| 1913 |
in the group follow the flag field. */ |
| 1914 |
static int |
| 1915 |
read_gs_section_group( |
| 1916 |
dwarf_elf_object_access_internals_t *ep, |
| 1917 |
struct generic_shdr* psh, |
| 1918 |
int *errcode) |
| 1919 |
{ |
| 1920 |
Dwarf_Unsigned i = 0; |
| 1921 |
int res = 0; |
| 1922 |
|
| 1923 |
if (!psh->gh_sht_group_array) { |
| 1924 |
Dwarf_Unsigned seclen = psh->gh_size; |
| 1925 |
char *data = 0; |
| 1926 |
char *dp = 0; |
| 1927 |
Dwarf_Unsigned* grouparray = 0; |
| 1928 |
char dblock[4]; |
| 1929 |
Dwarf_Unsigned va = 0; |
| 1930 |
Dwarf_Unsigned count = 0; |
| 1931 |
Dwarf_Unsigned groupmallocsize = 0; |
| 1932 |
int foundone = 0; |
| 1933 |
|
| 1934 |
if (seclen >= ep->f_filesize) { |
| 1935 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1936 |
return DW_DLV_ERROR; |
| 1937 |
} |
| 1938 |
if (seclen < DWARF_32BIT_SIZE) { |
| 1939 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1940 |
return DW_DLV_ERROR; |
| 1941 |
} |
| 1942 |
data = malloc(seclen); |
| 1943 |
if (!data) { |
| 1944 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1945 |
return DW_DLV_ERROR; |
| 1946 |
} |
| 1947 |
dp = data; |
| 1948 |
if (psh->gh_entsize != DWARF_32BIT_SIZE) { |
| 1949 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1950 |
free(data); |
| 1951 |
return DW_DLV_ERROR; |
| 1952 |
} |
| 1953 |
if (!psh->gh_entsize) { |
| 1954 |
free(data); |
| 1955 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1956 |
return DW_DLV_ERROR; |
| 1957 |
} |
| 1958 |
count = seclen/psh->gh_entsize; |
| 1959 |
if (count >= ep->f_loc_shdr.g_count) { |
| 1960 |
/* Impossible */ |
| 1961 |
free(data); |
| 1962 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1963 |
return DW_DLV_ERROR; |
| 1964 |
} |
| 1965 |
res = RRMOA(ep->f_fd,data,psh->gh_offset,seclen, |
| 1966 |
ep->f_filesize,errcode); |
| 1967 |
if (res != DW_DLV_OK) { |
| 1968 |
free(data); |
| 1969 |
return res; |
| 1970 |
} |
| 1971 |
/* Adding 1 is silly but possibly avoids a warning |
| 1972 |
from a particular compiler. */ |
| 1973 |
groupmallocsize = (1+count) * sizeof(Dwarf_Unsigned); |
| 1974 |
if (groupmallocsize >= ep->f_filesize) { |
| 1975 |
free(data); |
| 1976 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1977 |
return DW_DLV_ERROR; |
| 1978 |
} |
| 1979 |
grouparray = malloc(groupmallocsize); |
| 1980 |
if (!grouparray) { |
| 1981 |
free(data); |
| 1982 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1983 |
return DW_DLV_ERROR; |
| 1984 |
} |
| 1985 |
|
| 1986 |
memcpy(dblock,dp,DWARF_32BIT_SIZE); |
| 1987 |
ASNAR(memcpy,va,dblock); |
| 1988 |
/* There is ambiguity on the endianness of this stuff. */ |
| 1989 |
if (va != 1 && va != 0x1000000) { |
| 1990 |
/* Could be corrupted elf object. */ |
| 1991 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 1992 |
free(data); |
| 1993 |
free(grouparray); |
| 1994 |
return DW_DLV_ERROR; |
| 1995 |
} |
| 1996 |
grouparray[0] = 1; |
| 1997 |
/* A .group section will have 0 to G sections |
| 1998 |
listed. Ignore the initial 'version' value |
| 1999 |
of 1 in [0] */ |
| 2000 |
dp = dp + DWARF_32BIT_SIZE; |
| 2001 |
for ( i = 1; i < count; ++i,dp += DWARF_32BIT_SIZE) { |
| 2002 |
Dwarf_Unsigned gseca = 0; |
| 2003 |
Dwarf_Unsigned gsecb = 0; |
| 2004 |
struct generic_shdr* targpsh = 0; |
| 2005 |
|
| 2006 |
memcpy(dblock,dp,DWARF_32BIT_SIZE); |
| 2007 |
ASNAR(memcpy,gseca,dblock); |
| 2008 |
/* Loading gseca and gsecb with different endianness. |
| 2009 |
Only one of them can be of any use. */ |
| 2010 |
ASNAR(_dwarf_memcpy_swap_bytes,gsecb,dblock); |
| 2011 |
if (!gseca) { |
| 2012 |
/* zero! Oops. No point in looking at gsecb */ |
| 2013 |
free(data); |
| 2014 |
free(grouparray); |
| 2015 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 2016 |
return DW_DLV_ERROR; |
| 2017 |
} |
| 2018 |
if (gseca >= ep->f_loc_shdr.g_count) { |
| 2019 |
/* Might be confused endianness by |
| 2020 |
the compiler generating the SHT_GROUP. |
| 2021 |
This is pretty horrible. */ |
| 2022 |
if (gsecb >= ep->f_loc_shdr.g_count) { |
| 2023 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 2024 |
free(data); |
| 2025 |
free(grouparray); |
| 2026 |
return DW_DLV_ERROR; |
| 2027 |
} |
| 2028 |
/* Looks as though gsecb is the correct |
| 2029 |
interpretation. Yes, ugly. */ |
| 2030 |
gseca = gsecb; |
| 2031 |
} |
| 2032 |
grouparray[i] = gseca; |
| 2033 |
targpsh = ep->f_shdr + gseca; |
| 2034 |
if (_dwarf_ignorethissection(targpsh->gh_namestring)){ |
| 2035 |
continue; |
| 2036 |
} |
| 2037 |
if (targpsh->gh_section_group_number) { |
| 2038 |
/* multi-assignment to groups. Oops. */ |
| 2039 |
free(data); |
| 2040 |
free(grouparray); |
| 2041 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 2042 |
return DW_DLV_ERROR; |
| 2043 |
} |
| 2044 |
targpsh->gh_section_group_number = |
| 2045 |
ep->f_sg_next_group_number; |
| 2046 |
foundone = 1; |
| 2047 |
} |
| 2048 |
if (foundone) { |
| 2049 |
++ep->f_sg_next_group_number; |
| 2050 |
++ep->f_sht_group_type_section_count; |
| 2051 |
} |
| 2052 |
free(data); |
| 2053 |
psh->gh_sht_group_array = grouparray; |
| 2054 |
psh->gh_sht_group_array_count = count; |
| 2055 |
} |
| 2056 |
return DW_DLV_OK; |
| 2057 |
} |
| 2058 |
/* Does related things. |
| 2059 |
A) Counts the number of SHT_GROUP |
| 2060 |
and for each builds an array of the sections in the group |
| 2061 |
(which we expect are all DWARF-related) |
| 2062 |
and sets the group number in each mentioned section. |
| 2063 |
B) Counts the number of SHF_GROUP flags. |
| 2064 |
C) If gnu groups: |
| 2065 |
ensure all the DWARF sections marked with right group |
| 2066 |
based on A(we will mark unmarked as group 1, |
| 2067 |
DW_GROUPNUMBER_BASE). |
| 2068 |
D) If arm groups (SHT_GROUP zero, SHF_GROUP non-zero): |
| 2069 |
Check the relocations of all SHF_GROUP section |
| 2070 |
FIXME: algorithm needed. |
| 2071 |
|
| 2072 |
If SHT_GROUP and SHF_GROUP this is GNU groups. |
| 2073 |
If no SHT_GROUP and have SHF_GROUP this is |
| 2074 |
arm cc groups and we must use relocation information |
| 2075 |
to identify the group members. |
| 2076 |
|
| 2077 |
It seems(?) impossible for an object to have both |
| 2078 |
dwo sections and (SHF_GROUP or SHT_GROUP), but |
| 2079 |
we do not rule that out here. */ |
| 2080 |
static int |
| 2081 |
_dwarf_elf_setup_all_section_groups( |
| 2082 |
dwarf_elf_object_access_internals_t *ep, |
| 2083 |
int *errcode) |
| 2084 |
{ |
| 2085 |
struct generic_shdr* psh = 0; |
| 2086 |
Dwarf_Unsigned i = 0; |
| 2087 |
Dwarf_Unsigned count = 0; |
| 2088 |
int res = 0; |
| 2089 |
|
| 2090 |
count = ep->f_loc_shdr.g_count; |
| 2091 |
psh = ep->f_shdr; |
| 2092 |
|
| 2093 |
/* Does step A and step B */ |
| 2094 |
for (i = 0; i < count; ++psh,++i) { |
| 2095 |
const char *name = psh->gh_namestring; |
| 2096 |
|
| 2097 |
if (is_empty_section(psh->gh_type)) { |
| 2098 |
/* No data here. */ |
| 2099 |
continue; |
| 2100 |
} |
| 2101 |
if (!elf_sht_groupsec(psh->gh_type,name)) { |
| 2102 |
/* Step B */ |
| 2103 |
if (elf_flagmatches(psh->gh_flags,SHF_GROUP)) { |
| 2104 |
ep->f_shf_group_flag_section_count++; |
| 2105 |
} |
| 2106 |
continue; |
| 2107 |
} |
| 2108 |
/* Looks like a section group. Do Step A. */ |
| 2109 |
res =read_gs_section_group(ep,psh,errcode); |
| 2110 |
if (res != DW_DLV_OK) { |
| 2111 |
return res; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
/* Any sections not marked above or here are in |
| 2115 |
grep DW_GROUPNUMBER_BASE (1). |
| 2116 |
Section C. */ |
| 2117 |
psh = ep->f_shdr; |
| 2118 |
for (i = 0; i < count; ++psh,++i) { |
| 2119 |
const char *name = psh->gh_namestring; |
| 2120 |
int is_rel = FALSE; |
| 2121 |
int is_rela = FALSE; |
| 2122 |
|
| 2123 |
if (is_empty_section(psh->gh_type)) { |
| 2124 |
/* No data here. */ |
| 2125 |
continue; |
| 2126 |
} |
| 2127 |
if (elf_sht_groupsec(psh->gh_type,name)) { |
| 2128 |
continue; |
| 2129 |
} |
| 2130 |
/* Not a section group */ |
| 2131 |
if (string_endswith(name,".dwo")) { |
| 2132 |
if (psh->gh_section_group_number) { |
| 2133 |
/* multi-assignment to groups. Oops. */ |
| 2134 |
*errcode = DW_DLE_ELF_SECTION_GROUP_ERROR; |
| 2135 |
return DW_DLV_ERROR; |
| 2136 |
} |
| 2137 |
psh->gh_is_dwarf = TRUE; |
| 2138 |
psh->gh_section_group_number = DW_GROUPNUMBER_DWO; |
| 2139 |
ep->f_dwo_group_section_count++; |
| 2140 |
} else if (_dwarf_load_elf_section_is_dwarf(name, |
| 2141 |
psh->gh_type, |
| 2142 |
&is_rela,&is_rel)) { |
| 2143 |
if (!psh->gh_section_group_number) { |
| 2144 |
psh->gh_section_group_number = DW_GROUPNUMBER_BASE; |
| 2145 |
} |
| 2146 |
psh->gh_is_dwarf = TRUE; |
| 2147 |
} else { |
| 2148 |
/* Do nothing. */ |
| 2149 |
} |
| 2150 |
} |
| 2151 |
if (ep->f_sht_group_type_section_count) { |
| 2152 |
/* Not ARM. Done. */ |
| 2153 |
} |
| 2154 |
if (!ep->f_shf_group_flag_section_count) { |
| 2155 |
/* Nothing more to do. */ |
| 2156 |
return DW_DLV_OK; |
| 2157 |
} |
| 2158 |
return DW_DLV_OK; |
| 2159 |
} |
| 2160 |
|
| 2161 |
static int |
| 2162 |
_dwarf_elf_find_sym_sections( |
| 2163 |
dwarf_elf_object_access_internals_t *ep, |
| 2164 |
int *errcode) |
| 2165 |
{ |
| 2166 |
struct generic_shdr* psh = 0; |
| 2167 |
Dwarf_Unsigned i = 0; |
| 2168 |
Dwarf_Unsigned count = 0; |
| 2169 |
int res = 0; |
| 2170 |
|
| 2171 |
count = ep->f_loc_shdr.g_count; |
| 2172 |
psh = ep->f_shdr; |
| 2173 |
for (i = 0; i < count; ++psh,++i) { |
| 2174 |
const char *name = psh->gh_namestring; |
| 2175 |
if (is_empty_section(psh->gh_type)) { |
| 2176 |
/* No data here. */ |
| 2177 |
continue; |
| 2178 |
} |
| 2179 |
if (!strcmp(name,".dynsym")) { |
| 2180 |
ep->f_dynsym_sect_index = i; |
| 2181 |
ep->f_loc_dynsym.g_offset = psh->gh_offset; |
| 2182 |
} else if (!strcmp(name,".dynstr")) { |
| 2183 |
ep->f_dynsym_sect_strings_sect_index = i; |
| 2184 |
ep->f_dynsym_sect_strings_max = psh->gh_size; |
| 2185 |
} else if (!strcmp(name,".symtab")) { |
| 2186 |
ep->f_symtab_sect_index = i; |
| 2187 |
ep->f_loc_symtab.g_offset = psh->gh_offset; |
| 2188 |
} else if (!strcmp(name,".strtab")) { |
| 2189 |
ep->f_symtab_sect_strings_sect_index = i; |
| 2190 |
ep->f_symtab_sect_strings_max = psh->gh_size; |
| 2191 |
} else if (!strcmp(name,".dynamic")) { |
| 2192 |
ep->f_dynamic_sect_index = i; |
| 2193 |
ep->f_loc_dynamic.g_offset = psh->gh_offset; |
| 2194 |
} |
| 2195 |
} |
| 2196 |
res = validate_links(ep,ep->f_symtab_sect_index, |
| 2197 |
ep->f_symtab_sect_strings_sect_index,errcode); |
| 2198 |
if (res!= DW_DLV_OK) { |
| 2199 |
return res; |
| 2200 |
} |
| 2201 |
return DW_DLV_OK; |
| 2202 |
} |
| 2203 |
|
| 2204 |
int |
| 2205 |
_dwarf_load_elf_sectheaders( |
| 2206 |
dwarf_elf_object_access_internals_t *ep,int*errcode) |
| 2207 |
{ |
| 2208 |
int res = 0; |
| 2209 |
|
| 2210 |
if (ep->f_offsetsize == 32) { |
| 2211 |
res = elf_load_sectheaders32(ep,ep->f_ehdr->ge_shoff, |
| 2212 |
ep->f_ehdr->ge_shentsize, |
| 2213 |
ep->f_ehdr->ge_shnum,errcode); |
| 2214 |
} else if (ep->f_offsetsize == 64) { |
| 2215 |
res = elf_load_sectheaders64(ep,ep->f_ehdr->ge_shoff, |
| 2216 |
ep->f_ehdr->ge_shentsize, |
| 2217 |
ep->f_ehdr->ge_shnum,errcode); |
| 2218 |
} else { |
| 2219 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 2220 |
return DW_DLV_ERROR; |
| 2221 |
} |
| 2222 |
if (res != DW_DLV_OK) { |
| 2223 |
return res; |
| 2224 |
} |
| 2225 |
res = _dwarf_elf_load_sectstrings(ep, |
| 2226 |
ep->f_ehdr->ge_shstrndx,errcode); |
| 2227 |
if (res != DW_DLV_OK) { |
| 2228 |
return res; |
| 2229 |
} |
| 2230 |
res = _dwarf_elf_load_sect_namestring(ep,errcode); |
| 2231 |
if (res != DW_DLV_OK) { |
| 2232 |
return res; |
| 2233 |
} |
| 2234 |
res = _dwarf_elf_find_sym_sections(ep,errcode); |
| 2235 |
if (res != DW_DLV_OK) { |
| 2236 |
return res; |
| 2237 |
} |
| 2238 |
res = _dwarf_elf_setup_all_section_groups(ep,errcode); |
| 2239 |
return res; |
| 2240 |
} |