| 1 |
/* |
| 2 |
Copyright (c) 2020-2021, David Anderson All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
/* This file reads the parts of a Windows PE |
| 33 |
file appropriate to reading DWARF debugging data. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include <config.h> |
| 37 |
#include <stddef.h> /* size_t */ |
| 38 |
#include <stdlib.h> /* atoi() calloc() free() malloc() */ |
| 39 |
#include <string.h> /* memset() strdup() strlen() */ |
| 40 |
|
| 41 |
#include "dwarf.h" |
| 42 |
#include "libdwarf.h" |
| 43 |
#include "libdwarf_private.h" |
| 44 |
#include "dwarf_base_types.h" |
| 45 |
#include "dwarf_safe_strcpy.h" |
| 46 |
#include "dwarf_opaque.h" |
| 47 |
#include "dwarf_memcpy_swap.h" |
| 48 |
#include "dwarf_error.h" /* for _dwarf_error() declaration */ |
| 49 |
#include "dwarf_reading.h" |
| 50 |
#include "dwarf_object_read_common.h" |
| 51 |
#include "dwarf_object_detector.h" |
| 52 |
#include "dwarf_pe_descr.h" |
| 53 |
#include "dwarf_peread.h" |
| 54 |
|
| 55 |
#define DOS_HEADER_LEN 64 |
| 56 |
|
| 57 |
#if 0 /* for debugging. */ |
| 58 |
static void |
| 59 |
dump_bytes(char * msg,Dwarf_Small * start, long len) |
| 60 |
{ |
| 61 |
Dwarf_Small *end = start + len; |
| 62 |
Dwarf_Small *cur = start; |
| 63 |
|
| 64 |
printf("%s len %ld ",msg,len); |
| 65 |
for (; cur < end; cur++) { |
| 66 |
printf("%02x ", *cur); |
| 67 |
} |
| 68 |
printf("\n"); |
| 69 |
} |
| 70 |
#endif /*0*/ |
| 71 |
|
| 72 |
static int _dwarf_pe_object_access_init( |
| 73 |
int fd, |
| 74 |
unsigned ftype, |
| 75 |
unsigned endian, |
| 76 |
unsigned offsetsize, |
| 77 |
size_t filesize, |
| 78 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 79 |
int *localerrnum); |
| 80 |
|
| 81 |
static unsigned long |
| 82 |
magic_copy(char *d, unsigned len) |
| 83 |
{ |
| 84 |
unsigned i = 0; |
| 85 |
unsigned long v = 0; |
| 86 |
|
| 87 |
v = d[0]; |
| 88 |
for (i = 1 ; i < len; ++i) { |
| 89 |
v <<= 8; |
| 90 |
v |= 0xff&d[i]; |
| 91 |
} |
| 92 |
return v; |
| 93 |
} |
| 94 |
static int |
| 95 |
check_valid_string(char *tab, |
| 96 |
Dwarf_Unsigned size, |
| 97 |
Dwarf_Unsigned startindex) |
| 98 |
{ |
| 99 |
Dwarf_Unsigned i = startindex; |
| 100 |
for ( ; i < size; ++i) { |
| 101 |
if (!tab[i]) { |
| 102 |
return DW_DLV_OK; |
| 103 |
} |
| 104 |
} |
| 105 |
return DW_DLV_ERROR; |
| 106 |
} |
| 107 |
|
| 108 |
/* Name_array is 8 byte string, or it is supposed to be |
| 109 |
anyway. */ |
| 110 |
static int |
| 111 |
pe_section_name_get(dwarf_pe_object_access_internals_t *pep, |
| 112 |
const char *name_array, |
| 113 |
|
| 114 |
/* The name strlen */ |
| 115 |
unsigned long size_name, |
| 116 |
const char ** name_out, |
| 117 |
int *errcode) |
| 118 |
{ |
| 119 |
if (name_array[0] == '/') { |
| 120 |
long v = 0; |
| 121 |
unsigned long u = 0; |
| 122 |
const char *s = 0; |
| 123 |
char temp_array[9]; |
| 124 |
int res = 0; |
| 125 |
|
| 126 |
/* The value is an integer after the /, |
| 127 |
and we want the value */ |
| 128 |
_dwarf_safe_strcpy(temp_array,sizeof(temp_array), |
| 129 |
name_array+1,size_name-1); |
| 130 |
v = atoi(temp_array); |
| 131 |
if (v < 0) { |
| 132 |
*errcode = DW_DLE_STRING_OFFSET_BAD; |
| 133 |
return DW_DLV_ERROR; |
| 134 |
} |
| 135 |
u = v; |
| 136 |
if (!pep->pe_string_table) { |
| 137 |
*errcode = DW_DLE_STRING_OFFSET_BAD; |
| 138 |
return DW_DLV_ERROR; |
| 139 |
} |
| 140 |
if (u >= pep->pe_string_table_size) { |
| 141 |
*errcode = DW_DLE_STRING_OFFSET_BAD; |
| 142 |
return DW_DLV_ERROR; |
| 143 |
} |
| 144 |
res = check_valid_string(pep->pe_string_table, |
| 145 |
pep->pe_string_table_size,u); |
| 146 |
if (res != DW_DLV_OK) { |
| 147 |
*errcode = DW_DLE_STRING_OFFSET_BAD; |
| 148 |
return DW_DLV_ERROR; |
| 149 |
} |
| 150 |
s = pep->pe_string_table +u; |
| 151 |
*name_out = s; |
| 152 |
return DW_DLV_OK; |
| 153 |
} |
| 154 |
*name_out = name_array; |
| 155 |
return DW_DLV_OK; |
| 156 |
} |
| 157 |
|
| 158 |
static Dwarf_Small |
| 159 |
pe_get_byte_order (void *obj) |
| 160 |
{ |
| 161 |
dwarf_pe_object_access_internals_t *pep = |
| 162 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 163 |
return pep->pe_endian; |
| 164 |
} |
| 165 |
|
| 166 |
static Dwarf_Small |
| 167 |
pe_get_length_size (void *obj) |
| 168 |
{ |
| 169 |
dwarf_pe_object_access_internals_t *pep = |
| 170 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 171 |
return pep->pe_offsetsize/8; |
| 172 |
} |
| 173 |
|
| 174 |
static Dwarf_Unsigned |
| 175 |
pe_get_file_size (void *obj) |
| 176 |
{ |
| 177 |
dwarf_pe_object_access_internals_t *pep = |
| 178 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 179 |
return pep->pe_filesize; |
| 180 |
} |
| 181 |
|
| 182 |
static Dwarf_Small |
| 183 |
pe_get_pointer_size (void *obj) |
| 184 |
{ |
| 185 |
dwarf_pe_object_access_internals_t *pep = |
| 186 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 187 |
return pep->pe_pointersize/8; |
| 188 |
} |
| 189 |
|
| 190 |
static Dwarf_Unsigned |
| 191 |
pe_get_section_count (void *obj) |
| 192 |
{ |
| 193 |
dwarf_pe_object_access_internals_t *pep = |
| 194 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 195 |
return pep->pe_section_count; |
| 196 |
} |
| 197 |
|
| 198 |
static int |
| 199 |
pe_get_section_info (void *obj, |
| 200 |
Dwarf_Unsigned section_index, |
| 201 |
Dwarf_Obj_Access_Section_a *return_section, |
| 202 |
int *error) |
| 203 |
{ |
| 204 |
dwarf_pe_object_access_internals_t *pep = |
| 205 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 206 |
|
| 207 |
(void)error; |
| 208 |
if (section_index < pep->pe_section_count) { |
| 209 |
struct dwarf_pe_generic_image_section_header *sp = 0; |
| 210 |
sp = pep->pe_sectionptr + section_index; |
| 211 |
return_section->as_name = sp->dwarfsectname; |
| 212 |
return_section->as_type = 0; |
| 213 |
return_section->as_flags = sp->Characteristics; |
| 214 |
return_section->as_addr = pep->pe_OptionalHeader.ImageBase + |
| 215 |
sp->VirtualAddress; |
| 216 |
return_section->as_offset = sp->PointerToRawData; |
| 217 |
/* SizeOfRawData can be rounded or truncated, |
| 218 |
use VirtualSize for the real analog of Elf |
| 219 |
section size. */ |
| 220 |
return_section->as_size = sp->VirtualSize; |
| 221 |
return_section->as_link = 0; |
| 222 |
return_section->as_info = 0; |
| 223 |
return_section->as_addralign = 0; |
| 224 |
return_section->as_entrysize = 0; |
| 225 |
return DW_DLV_OK; |
| 226 |
} |
| 227 |
return DW_DLV_NO_ENTRY; |
| 228 |
} |
| 229 |
|
| 230 |
static int |
| 231 |
load_optional_header32(dwarf_pe_object_access_internals_t *pep, |
| 232 |
Dwarf_Unsigned offset, int*errcode) |
| 233 |
{ |
| 234 |
int res = 0; |
| 235 |
IMAGE_OPTIONAL_HEADER32_dw hdr; |
| 236 |
|
| 237 |
pep->pe_optional_header_size = sizeof(IMAGE_OPTIONAL_HEADER32_dw); |
| 238 |
|
| 239 |
if ((pep->pe_optional_header_size + offset) > |
| 240 |
pep->pe_filesize) { |
| 241 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 242 |
return DW_DLV_ERROR; |
| 243 |
} |
| 244 |
|
| 245 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 246 |
(char *)&hdr, |
| 247 |
offset, sizeof(IMAGE_OPTIONAL_HEADER32_dw), |
| 248 |
pep->pe_filesize, |
| 249 |
errcode); |
| 250 |
if (res != DW_DLV_OK) { |
| 251 |
return res; |
| 252 |
} |
| 253 |
|
| 254 |
/* This is a subset of fields. */ |
| 255 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.Magic, |
| 256 |
hdr.Magic); |
| 257 |
pep->pe_OptionalHeader.MajorLinkerVersion= hdr.MajorLinkerVersion; |
| 258 |
pep->pe_OptionalHeader.MinorLinkerVersion= hdr.MinorLinkerVersion; |
| 259 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.ImageBase, |
| 260 |
hdr.ImageBase); |
| 261 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfCode, |
| 262 |
hdr.SizeOfCode); |
| 263 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfImage, |
| 264 |
hdr.SizeOfImage); |
| 265 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfHeaders, |
| 266 |
hdr.SizeOfHeaders); |
| 267 |
pep->pe_OptionalHeader.SizeOfDataDirEntry = |
| 268 |
sizeof(IMAGE_DATA_DIRECTORY_dw); |
| 269 |
return DW_DLV_OK; |
| 270 |
} |
| 271 |
static int |
| 272 |
load_optional_header64(dwarf_pe_object_access_internals_t *pep, |
| 273 |
Dwarf_Unsigned offset, int*errcode ) |
| 274 |
{ |
| 275 |
IMAGE_OPTIONAL_HEADER64_dw hdr; |
| 276 |
int res = 0; |
| 277 |
|
| 278 |
pep->pe_optional_header_size = sizeof(IMAGE_OPTIONAL_HEADER64_dw); |
| 279 |
if ((pep->pe_optional_header_size + offset) > |
| 280 |
pep->pe_filesize) { |
| 281 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 282 |
return DW_DLV_ERROR; |
| 283 |
} |
| 284 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 285 |
(char *)&hdr, |
| 286 |
offset, sizeof(IMAGE_OPTIONAL_HEADER64_dw), |
| 287 |
pep->pe_filesize, |
| 288 |
errcode); |
| 289 |
if (res != DW_DLV_OK) { |
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
/* This is a subset of fields. */ |
| 294 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.Magic, |
| 295 |
hdr.Magic); |
| 296 |
pep->pe_OptionalHeader.MajorLinkerVersion= hdr.MajorLinkerVersion; |
| 297 |
pep->pe_OptionalHeader.MinorLinkerVersion= hdr.MinorLinkerVersion; |
| 298 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.ImageBase, |
| 299 |
hdr.ImageBase); |
| 300 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfCode, |
| 301 |
hdr.SizeOfCode); |
| 302 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfImage, |
| 303 |
hdr.SizeOfImage); |
| 304 |
ASNAR(pep->pe_copy_word,pep->pe_OptionalHeader.SizeOfHeaders, |
| 305 |
hdr.SizeOfHeaders); |
| 306 |
pep->pe_OptionalHeader.SizeOfDataDirEntry = |
| 307 |
sizeof(IMAGE_DATA_DIRECTORY_dw); |
| 308 |
return DW_DLV_OK; |
| 309 |
} |
| 310 |
|
| 311 |
static char *boringname[] = { |
| 312 |
".text", |
| 313 |
".bss", |
| 314 |
".data", |
| 315 |
".rdata", |
| 316 |
0 |
| 317 |
}; |
| 318 |
|
| 319 |
static int |
| 320 |
in_name_list(char * name) |
| 321 |
{ |
| 322 |
int i = 0; |
| 323 |
|
| 324 |
if (!name) { |
| 325 |
return FALSE; |
| 326 |
} |
| 327 |
for ( ; ; ++i) { |
| 328 |
if (!boringname[i]) { |
| 329 |
break; |
| 330 |
} |
| 331 |
if (!strcmp(name,boringname[i])) { |
| 332 |
return TRUE; |
| 333 |
} |
| 334 |
} |
| 335 |
return FALSE; |
| 336 |
} |
| 337 |
|
| 338 |
static int |
| 339 |
is_irrelevant_section(char * name, |
| 340 |
Dwarf_Unsigned virtsz) |
| 341 |
{ |
| 342 |
int res = FALSE; |
| 343 |
|
| 344 |
res = in_name_list(name); |
| 345 |
if (res) { |
| 346 |
return TRUE; |
| 347 |
} |
| 348 |
if (!virtsz) { |
| 349 |
return TRUE; |
| 350 |
} |
| 351 |
return FALSE; |
| 352 |
} |
| 353 |
|
| 354 |
static int |
| 355 |
pe_load_section (void *obj, Dwarf_Unsigned section_index, |
| 356 |
Dwarf_Small **return_data, int *error) |
| 357 |
{ |
| 358 |
dwarf_pe_object_access_internals_t *pep = |
| 359 |
(dwarf_pe_object_access_internals_t*)(obj); |
| 360 |
|
| 361 |
if (0 < section_index && |
| 362 |
section_index < pep->pe_section_count) { |
| 363 |
int res = 0; |
| 364 |
struct dwarf_pe_generic_image_section_header *sp = |
| 365 |
pep->pe_sectionptr + section_index; |
| 366 |
Dwarf_Unsigned read_length = 0; |
| 367 |
|
| 368 |
if (sp->loaded_data) { |
| 369 |
*return_data = sp->loaded_data; |
| 370 |
return DW_DLV_OK; |
| 371 |
} |
| 372 |
if (sp->section_irrelevant_to_dwarf) { |
| 373 |
return DW_DLV_NO_ENTRY; |
| 374 |
} |
| 375 |
if (!sp->VirtualSize) { |
| 376 |
return DW_DLV_NO_ENTRY; |
| 377 |
} |
| 378 |
if (sp->SizeOfRawData >= pep->pe_filesize) { |
| 379 |
*error = DW_DLE_PE_SECTION_SIZE_ERROR; |
| 380 |
return DW_DLV_ERROR; |
| 381 |
} |
| 382 |
read_length = sp->SizeOfRawData; |
| 383 |
if (sp->VirtualSize < read_length) { |
| 384 |
/* Don't read padding that wasn't allocated in memory */ |
| 385 |
read_length = sp->VirtualSize; |
| 386 |
} |
| 387 |
if ((read_length + sp->PointerToRawData) > |
| 388 |
pep->pe_filesize) { |
| 389 |
*error = DW_DLE_FILE_TOO_SMALL; |
| 390 |
return DW_DLV_ERROR; |
| 391 |
} |
| 392 |
/* VirtualSize > SizeOfRawData if trailing zeros |
| 393 |
in the section were not written to disc. |
| 394 |
Malloc enough for the whole section, read in |
| 395 |
the bytes we have. */ |
| 396 |
sp->loaded_data = malloc((size_t)sp->VirtualSize); |
| 397 |
if (!sp->loaded_data) { |
| 398 |
*error = DW_DLE_ALLOC_FAIL; |
| 399 |
return DW_DLV_ERROR; |
| 400 |
} |
| 401 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 402 |
(char *)sp->loaded_data, |
| 403 |
sp->PointerToRawData, (size_t)read_length, |
| 404 |
pep->pe_filesize, |
| 405 |
error); |
| 406 |
if (res != DW_DLV_OK) { |
| 407 |
free(sp->loaded_data); |
| 408 |
sp->loaded_data = 0; |
| 409 |
return res; |
| 410 |
} |
| 411 |
if (sp->VirtualSize > read_length) { |
| 412 |
/* Zero space that was allocated but |
| 413 |
truncated from the file */ |
| 414 |
memset(sp->loaded_data + read_length, 0, |
| 415 |
(size_t)(sp->VirtualSize - read_length)); |
| 416 |
} |
| 417 |
*return_data = sp->loaded_data; |
| 418 |
return DW_DLV_OK; |
| 419 |
} |
| 420 |
return DW_DLV_NO_ENTRY; |
| 421 |
} |
| 422 |
|
| 423 |
void |
| 424 |
_dwarf_destruct_pe_access( |
| 425 |
struct Dwarf_Obj_Access_Interface_a_s *aip) |
| 426 |
{ |
| 427 |
dwarf_pe_object_access_internals_t *pep = 0; |
| 428 |
Dwarf_Unsigned i = 0; |
| 429 |
|
| 430 |
if (!aip) { |
| 431 |
return; |
| 432 |
} |
| 433 |
pep = (dwarf_pe_object_access_internals_t*)(aip->ai_object); |
| 434 |
if (pep->pe_destruct_close_fd && pep->pe_fd !=-1) { |
| 435 |
_dwarf_closer(pep->pe_fd); |
| 436 |
pep->pe_fd = -1; |
| 437 |
} |
| 438 |
free((char *)pep->pe_path); |
| 439 |
pep->pe_path = 0; |
| 440 |
if (pep->pe_sectionptr) { |
| 441 |
struct dwarf_pe_generic_image_section_header *sp = 0; |
| 442 |
|
| 443 |
sp = pep->pe_sectionptr; |
| 444 |
for (i=0; i < pep->pe_section_count; ++i,++sp) { |
| 445 |
if (sp->loaded_data) { |
| 446 |
free(sp->loaded_data); |
| 447 |
sp->loaded_data = 0; |
| 448 |
} |
| 449 |
free(sp->name); |
| 450 |
sp->name = 0; |
| 451 |
free(sp->dwarfsectname); |
| 452 |
sp->dwarfsectname = 0; |
| 453 |
} |
| 454 |
free(pep->pe_sectionptr); |
| 455 |
pep->pe_section_count = 0; |
| 456 |
} |
| 457 |
free(pep->pe_string_table); |
| 458 |
pep->pe_string_table = 0; |
| 459 |
free(pep); |
| 460 |
free(aip); |
| 461 |
return; |
| 462 |
} |
| 463 |
|
| 464 |
static int |
| 465 |
_dwarf_pe_load_dwarf_section_headers( |
| 466 |
dwarf_pe_object_access_internals_t *pep,int *errcode) |
| 467 |
{ |
| 468 |
Dwarf_Unsigned i = 0; |
| 469 |
Dwarf_Unsigned input_count = |
| 470 |
pep->pe_FileHeader.NumberOfSections; |
| 471 |
Dwarf_Unsigned offset_in_input = pep->pe_section_table_offset; |
| 472 |
Dwarf_Unsigned section_hdr_size = sizeof(IMAGE_SECTION_HEADER_dw); |
| 473 |
struct dwarf_pe_generic_image_section_header *sec_outp = 0; |
| 474 |
Dwarf_Unsigned cur_offset = offset_in_input; |
| 475 |
Dwarf_Unsigned past_end_hdrs = offset_in_input + |
| 476 |
section_hdr_size*input_count; |
| 477 |
|
| 478 |
/* internal sections include null initial section */ |
| 479 |
pep->pe_section_count = input_count+1; |
| 480 |
|
| 481 |
if (past_end_hdrs > pep->pe_filesize) { |
| 482 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 483 |
return DW_DLV_ERROR; |
| 484 |
} |
| 485 |
|
| 486 |
if (!offset_in_input) { |
| 487 |
*errcode = DW_DLE_PE_OFFSET_BAD; |
| 488 |
return DW_DLV_ERROR; |
| 489 |
} |
| 490 |
pep->pe_sectionptr = |
| 491 |
(struct dwarf_pe_generic_image_section_header * ) |
| 492 |
calloc((size_t)pep->pe_section_count, |
| 493 |
sizeof(struct dwarf_pe_generic_image_section_header)); |
| 494 |
if (!pep->pe_sectionptr) { |
| 495 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 496 |
return DW_DLV_ERROR; |
| 497 |
} |
| 498 |
sec_outp = pep->pe_sectionptr; |
| 499 |
sec_outp->name = strdup(""); |
| 500 |
sec_outp->dwarfsectname = strdup(""); |
| 501 |
sec_outp++; |
| 502 |
for ( ; i < input_count; |
| 503 |
++i, cur_offset += section_hdr_size, sec_outp++) { |
| 504 |
|
| 505 |
int res = 0; |
| 506 |
IMAGE_SECTION_HEADER_dw filesect; |
| 507 |
char safe_name[IMAGE_SIZEOF_SHORT_NAME +1]; |
| 508 |
const char *expname = 0; |
| 509 |
int irrelevant = 0; |
| 510 |
|
| 511 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 512 |
(char *)&filesect,cur_offset, |
| 513 |
sizeof(filesect), |
| 514 |
pep->pe_filesize, |
| 515 |
errcode); |
| 516 |
if (res != DW_DLV_OK) { |
| 517 |
return res; |
| 518 |
} |
| 519 |
/* The following is safe. filesect.Name is |
| 520 |
IMAGE_SIZEOF_SHORT_NAME bytes long and may |
| 521 |
not (not sure) have a NUL terminator. */ |
| 522 |
_dwarf_safe_strcpy(safe_name, |
| 523 |
sizeof(safe_name), |
| 524 |
filesect.Name, |
| 525 |
IMAGE_SIZEOF_SHORT_NAME); |
| 526 |
/* Have NUL terminator now. */ |
| 527 |
sec_outp->name = strdup(safe_name); |
| 528 |
res = pe_section_name_get(pep, |
| 529 |
safe_name,(unsigned int)strlen(safe_name), |
| 530 |
&expname,errcode); |
| 531 |
if (res != DW_DLV_OK) { |
| 532 |
return res; |
| 533 |
} |
| 534 |
if (expname) { |
| 535 |
sec_outp->dwarfsectname = strdup(expname); |
| 536 |
} else { |
| 537 |
sec_outp->dwarfsectname = strdup("<sec name missing>"); |
| 538 |
} |
| 539 |
if ( !sec_outp->name || !sec_outp->dwarfsectname) { |
| 540 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 541 |
return DW_DLV_ERROR; |
| 542 |
} |
| 543 |
sec_outp->SecHeaderOffset = cur_offset; |
| 544 |
ASNAR(pep->pe_copy_word,sec_outp->VirtualSize, |
| 545 |
filesect.Misc.VirtualSize); |
| 546 |
ASNAR(pep->pe_copy_word,sec_outp->VirtualAddress, |
| 547 |
filesect.VirtualAddress); |
| 548 |
ASNAR(pep->pe_copy_word,sec_outp->SizeOfRawData, |
| 549 |
filesect.SizeOfRawData); |
| 550 |
irrelevant = is_irrelevant_section(sec_outp->dwarfsectname, |
| 551 |
sec_outp->VirtualSize); |
| 552 |
sec_outp->section_irrelevant_to_dwarf = irrelevant; |
| 553 |
if (irrelevant) { |
| 554 |
continue; |
| 555 |
} |
| 556 |
{ |
| 557 |
/* A Heuristic, allowing large virtual size |
| 558 |
but not unlimited as we will malloc it |
| 559 |
later, as Virtualsize. */ |
| 560 |
Dwarf_Unsigned limit = 100*pep->pe_filesize; |
| 561 |
if (limit < pep->pe_filesize) { |
| 562 |
/* An overflow. Bad. */ |
| 563 |
*errcode = DW_DLE_PE_SECTION_SIZE_HEURISTIC_FAIL; |
| 564 |
return DW_DLV_ERROR; |
| 565 |
} |
| 566 |
if (sec_outp->VirtualSize > |
| 567 |
((Dwarf_Unsigned)2000* |
| 568 |
(Dwarf_Unsigned)1000* |
| 569 |
(Dwarf_Unsigned)1000) && |
| 570 |
(sec_outp->VirtualSize > pep->pe_filesize)) { |
| 571 |
/* Likely unreasonable. |
| 572 |
the hard limit written this way |
| 573 |
simply for clarity. |
| 574 |
Hard to know what to set it to. */ |
| 575 |
*errcode = DW_DLE_PE_SECTION_SIZE_HEURISTIC_FAIL; |
| 576 |
return DW_DLV_ERROR; |
| 577 |
} |
| 578 |
if (sec_outp->VirtualSize > limit && |
| 579 |
0 == pep->pe_is_64bit ) { |
| 580 |
/* Likely totally unreasonable. Bad. */ |
| 581 |
*errcode = DW_DLE_PE_SECTION_SIZE_HEURISTIC_FAIL; |
| 582 |
return DW_DLV_ERROR; |
| 583 |
} |
| 584 |
} |
| 585 |
ASNAR(pep->pe_copy_word,sec_outp->PointerToRawData, |
| 586 |
filesect.PointerToRawData); |
| 587 |
if (sec_outp->SizeOfRawData > pep->pe_filesize || |
| 588 |
sec_outp->PointerToRawData > pep->pe_filesize || |
| 589 |
(sec_outp->SizeOfRawData+ |
| 590 |
sec_outp->PointerToRawData > pep->pe_filesize)) { |
| 591 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 592 |
return DW_DLV_ERROR; |
| 593 |
} |
| 594 |
ASNAR(pep->pe_copy_word,sec_outp->PointerToRelocations, |
| 595 |
filesect.PointerToRelocations); |
| 596 |
ASNAR(pep->pe_copy_word,sec_outp->PointerToLinenumbers, |
| 597 |
filesect.PointerToLinenumbers); |
| 598 |
ASNAR(pep->pe_copy_word,sec_outp->NumberOfRelocations, |
| 599 |
filesect.NumberOfRelocations); |
| 600 |
ASNAR(pep->pe_copy_word,sec_outp->NumberOfLinenumbers, |
| 601 |
filesect.NumberOfLinenumbers); |
| 602 |
ASNAR(pep->pe_copy_word,sec_outp->Characteristics, |
| 603 |
filesect.Characteristics); |
| 604 |
/* sec_outp->loaded data set when we load a section */ |
| 605 |
} |
| 606 |
return DW_DLV_OK; |
| 607 |
} |
| 608 |
|
| 609 |
static int |
| 610 |
_dwarf_load_pe_sections( |
| 611 |
dwarf_pe_object_access_internals_t *pep,int *errcode) |
| 612 |
{ |
| 613 |
struct dos_header_dw dhinmem; |
| 614 |
IMAGE_FILE_HEADER_dw ifh; |
| 615 |
void (*word_swap) (void *, const void *, unsigned long); |
| 616 |
unsigned locendian = 0; |
| 617 |
int res = 0; |
| 618 |
Dwarf_Unsigned dos_sig = 0; |
| 619 |
Dwarf_Unsigned nt_address = 0; |
| 620 |
char nt_sig_array[4]; |
| 621 |
unsigned long nt_signature = 0; |
| 622 |
|
| 623 |
if ( (sizeof(ifh) + sizeof(dhinmem)) >= pep->pe_filesize) { |
| 624 |
/* corrupt object. */ |
| 625 |
*errcode = DW_DLE_PE_SIZE_SMALL; |
| 626 |
return DW_DLV_ERROR; |
| 627 |
} |
| 628 |
res = _dwarf_object_read_random(pep->pe_fd,(char *)&dhinmem, |
| 629 |
0, sizeof(dhinmem),pep->pe_filesize, errcode); |
| 630 |
if (res != DW_DLV_OK) { |
| 631 |
return res; |
| 632 |
} |
| 633 |
dos_sig = magic_copy((char *)dhinmem.dh_mz, |
| 634 |
sizeof(dhinmem.dh_mz)); |
| 635 |
if (dos_sig == IMAGE_DOS_SIGNATURE_dw) { |
| 636 |
/* IMAGE_DOS_SIGNATURE_dw assumes bytes |
| 637 |
reversed by little-endian |
| 638 |
load, so we intrepet a match the other way. */ |
| 639 |
/* BIG ENDIAN. From looking at hex characters in object */ |
| 640 |
#ifdef WORDS_BIGENDIAN |
| 641 |
word_swap = _dwarf_memcpy_noswap_bytes; |
| 642 |
#else /* LITTLE ENDIAN */ |
| 643 |
word_swap = _dwarf_memcpy_swap_bytes; |
| 644 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 645 |
locendian = DW_END_big; |
| 646 |
} else if (dos_sig == IMAGE_DOS_REVSIGNATURE_dw) { |
| 647 |
/* raw load, so intrepet a match the other way. */ |
| 648 |
/* LITTLE ENDIAN */ |
| 649 |
#ifdef WORDS_BIGENDIAN |
| 650 |
word_swap = _dwarf_memcpy_swap_bytes; |
| 651 |
#else /* LITTLE ENDIAN */ |
| 652 |
word_swap = _dwarf_memcpy_noswap_bytes; |
| 653 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 654 |
locendian = DW_END_little; |
| 655 |
} else { |
| 656 |
/* Not dos header not a PE file we recognize */ |
| 657 |
*errcode = DW_DLE_FILE_WRONG_TYPE; |
| 658 |
return DW_DLV_ERROR; |
| 659 |
} |
| 660 |
if (locendian != pep->pe_endian) { |
| 661 |
/* Really this is a coding botch somewhere here, |
| 662 |
not an object corruption. */ |
| 663 |
*errcode = DW_DLE_FILE_WRONG_TYPE; |
| 664 |
return DW_DLV_ERROR; |
| 665 |
} |
| 666 |
pep->pe_copy_word = word_swap; |
| 667 |
ASNAR(word_swap,nt_address,dhinmem.dh_image_offset); |
| 668 |
if (pep->pe_filesize < (nt_address + sizeof(nt_sig_array))) { |
| 669 |
/* The nt_address is really a file offset. */ |
| 670 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 671 |
/* Not dos header not a PE file we recognize */ |
| 672 |
return DW_DLV_ERROR; |
| 673 |
} |
| 674 |
|
| 675 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 676 |
(char *)&nt_sig_array[0], |
| 677 |
nt_address, sizeof(nt_sig_array), |
| 678 |
pep->pe_filesize,errcode); |
| 679 |
if (res != DW_DLV_OK) { |
| 680 |
return res; |
| 681 |
} |
| 682 |
{ unsigned long lsig = 0; |
| 683 |
|
| 684 |
ASNAR(word_swap,lsig,nt_sig_array); |
| 685 |
nt_signature = lsig; |
| 686 |
} |
| 687 |
if (nt_signature != IMAGE_NT_SIGNATURE_dw) { |
| 688 |
*errcode = DW_DLE_FILE_WRONG_TYPE; |
| 689 |
return DW_DLV_ERROR; |
| 690 |
} |
| 691 |
|
| 692 |
pep->pe_nt_header_offset = nt_address + SIZEOFT32; |
| 693 |
if (pep->pe_filesize < (pep->pe_nt_header_offset + |
| 694 |
sizeof(ifh))) { |
| 695 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 696 |
/* Not image header not a PE file we recognize */ |
| 697 |
return DW_DLV_ERROR; |
| 698 |
} |
| 699 |
res = _dwarf_object_read_random(pep->pe_fd,(char *)&ifh, |
| 700 |
pep->pe_nt_header_offset, sizeof(ifh), |
| 701 |
pep->pe_filesize,errcode); |
| 702 |
if (res != DW_DLV_OK) { |
| 703 |
return res; |
| 704 |
} |
| 705 |
ASNAR(word_swap,pep->pe_FileHeader.Machine,ifh.Machine); |
| 706 |
ASNAR(word_swap,pep->pe_FileHeader.NumberOfSections, |
| 707 |
ifh.NumberOfSections); |
| 708 |
ASNAR(word_swap,pep->pe_FileHeader.TimeDateStamp, |
| 709 |
ifh.TimeDateStamp); |
| 710 |
ASNAR(word_swap,pep->pe_FileHeader.PointerToSymbolTable, |
| 711 |
ifh.PointerToSymbolTable); |
| 712 |
ASNAR(word_swap,pep->pe_FileHeader.NumberOfSymbols, |
| 713 |
ifh.NumberOfSymbols); |
| 714 |
ASNAR(word_swap,pep->pe_FileHeader.SizeOfOptionalHeader, |
| 715 |
ifh.SizeOfOptionalHeader); |
| 716 |
ASNAR(word_swap,pep->pe_FileHeader.Characteristics, |
| 717 |
ifh.Characteristics); |
| 718 |
pep->pe_machine = pep->pe_FileHeader.Machine; |
| 719 |
pep->pe_flags = pep->pe_FileHeader.Characteristics; |
| 720 |
pep->pe_optional_header_offset = pep->pe_nt_header_offset+ |
| 721 |
sizeof(ifh); |
| 722 |
if (pep->pe_offsetsize == 32) { |
| 723 |
res = load_optional_header32(pep, |
| 724 |
pep->pe_optional_header_offset,errcode); |
| 725 |
pep->pe_optional_header_size = |
| 726 |
sizeof(IMAGE_OPTIONAL_HEADER32_dw); |
| 727 |
} else if (pep->pe_offsetsize == 64) { |
| 728 |
res = load_optional_header64(pep, |
| 729 |
pep->pe_optional_header_offset,errcode); |
| 730 |
pep->pe_optional_header_size = |
| 731 |
sizeof(IMAGE_OPTIONAL_HEADER64_dw); |
| 732 |
} else { |
| 733 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 734 |
return DW_DLV_ERROR; |
| 735 |
} |
| 736 |
if (res != DW_DLV_OK) { |
| 737 |
return res; |
| 738 |
} |
| 739 |
|
| 740 |
pep->pe_section_table_offset = pep->pe_optional_header_offset |
| 741 |
+ pep->pe_optional_header_size; |
| 742 |
pep->pe_symbol_table_offset = |
| 743 |
pep->pe_FileHeader.PointerToSymbolTable; |
| 744 |
if (pep->pe_symbol_table_offset >= pep->pe_filesize) { |
| 745 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 746 |
return DW_DLV_ERROR; |
| 747 |
} |
| 748 |
if (pep->pe_symbol_table_offset) { |
| 749 |
pep->pe_string_table_offset = |
| 750 |
pep->pe_symbol_table_offset + |
| 751 |
(pep->pe_FileHeader.NumberOfSymbols * |
| 752 |
IMAGE_SIZEOF_SYMBOL); |
| 753 |
} |
| 754 |
|
| 755 |
if (pep->pe_string_table_offset >= pep->pe_filesize) { |
| 756 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 757 |
pep->pe_string_table_size = 0; |
| 758 |
return DW_DLV_ERROR; |
| 759 |
} |
| 760 |
if (pep->pe_string_table_offset) { |
| 761 |
/* https://docs.microsoft.com/en-us/\ |
| 762 |
windows/desktop/debug/pe-format#coff-string-table */ |
| 763 |
/* The first 4 bytes of the string table contain |
| 764 |
the size of the string table. */ |
| 765 |
char size_field[4]; |
| 766 |
|
| 767 |
if ((pep->pe_string_table_offset+sizeof(size_field)) > |
| 768 |
pep->pe_filesize) { |
| 769 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 770 |
return DW_DLV_ERROR; |
| 771 |
} |
| 772 |
memset(size_field,0,sizeof(size_field)); |
| 773 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 774 |
(char *)size_field, pep->pe_string_table_offset, |
| 775 |
sizeof(size_field), |
| 776 |
pep->pe_filesize,errcode); |
| 777 |
if (res != DW_DLV_OK) { |
| 778 |
return res; |
| 779 |
} |
| 780 |
ASNAR(pep->pe_copy_word,pep->pe_string_table_size, |
| 781 |
size_field); |
| 782 |
if (pep->pe_string_table_size >= pep->pe_filesize ) { |
| 783 |
*errcode = DW_DLE_PE_OFFSET_BAD; |
| 784 |
return DW_DLV_ERROR; |
| 785 |
} |
| 786 |
if ((pep->pe_string_table_offset+pep->pe_string_table_size) > |
| 787 |
pep->pe_filesize) { |
| 788 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 789 |
return DW_DLV_ERROR; |
| 790 |
} |
| 791 |
/* size+1 to ensure there is a terminating null character |
| 792 |
in memory so CoverityScan knows there is always a |
| 793 |
final null. CoverityScan is not aware |
| 794 |
there may be multiple strings in the table. |
| 795 |
If there is a compiler bug the final string |
| 796 |
might be missing its intended null terminator! */ |
| 797 |
pep->pe_string_table = |
| 798 |
(char *)calloc(1,(size_t)pep->pe_string_table_size+1); |
| 799 |
if (!pep->pe_string_table) { |
| 800 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 801 |
return DW_DLV_ERROR; |
| 802 |
} |
| 803 |
res = _dwarf_object_read_random(pep->pe_fd, |
| 804 |
(char *)pep->pe_string_table, |
| 805 |
pep->pe_string_table_offset, |
| 806 |
(size_t)pep->pe_string_table_size, |
| 807 |
pep->pe_filesize,errcode); |
| 808 |
if (res != DW_DLV_OK) { |
| 809 |
free(pep->pe_string_table); |
| 810 |
pep->pe_string_table = 0; |
| 811 |
return res; |
| 812 |
} |
| 813 |
/* Should pass coverity now. */ |
| 814 |
pep->pe_string_table[pep->pe_string_table_size] = 0; |
| 815 |
} |
| 816 |
res = _dwarf_pe_load_dwarf_section_headers(pep,errcode); |
| 817 |
return res; |
| 818 |
} |
| 819 |
|
| 820 |
int |
| 821 |
_dwarf_pe_setup(int fd, |
| 822 |
char *true_path, |
| 823 |
unsigned ftype, |
| 824 |
unsigned endian, |
| 825 |
unsigned offsetsize, |
| 826 |
size_t filesize, |
| 827 |
unsigned groupnumber, |
| 828 |
Dwarf_Handler errhand, |
| 829 |
Dwarf_Ptr errarg, |
| 830 |
Dwarf_Debug *dbg,Dwarf_Error *error) |
| 831 |
{ |
| 832 |
Dwarf_Obj_Access_Interface_a *binary_interface = 0; |
| 833 |
dwarf_pe_object_access_internals_t *pep = 0; |
| 834 |
int res = DW_DLV_OK; |
| 835 |
int localerrnum = 0; |
| 836 |
|
| 837 |
res = _dwarf_pe_object_access_init( |
| 838 |
fd, |
| 839 |
ftype,endian,offsetsize,filesize, |
| 840 |
&binary_interface, |
| 841 |
&localerrnum); |
| 842 |
if (res != DW_DLV_OK) { |
| 843 |
if (res == DW_DLV_NO_ENTRY) { |
| 844 |
return res; |
| 845 |
} |
| 846 |
_dwarf_error(NULL, error, localerrnum); |
| 847 |
return DW_DLV_ERROR; |
| 848 |
} |
| 849 |
/* allocates and initializes Dwarf_Debug, |
| 850 |
generic code */ |
| 851 |
res = dwarf_object_init_b(binary_interface, errhand, errarg, |
| 852 |
groupnumber, dbg, error); |
| 853 |
if (res != DW_DLV_OK){ |
| 854 |
_dwarf_destruct_pe_access(binary_interface); |
| 855 |
return res; |
| 856 |
} |
| 857 |
pep = binary_interface->ai_object; |
| 858 |
(*dbg)->de_obj_flags = pep->pe_flags; |
| 859 |
(*dbg)->de_obj_machine = pep->pe_machine; |
| 860 |
pep->pe_path = strdup(true_path); |
| 861 |
return res; |
| 862 |
} |
| 863 |
|
| 864 |
static Dwarf_Obj_Access_Methods_a pe_methods = { |
| 865 |
pe_get_section_info, |
| 866 |
pe_get_byte_order, |
| 867 |
pe_get_length_size, |
| 868 |
pe_get_pointer_size, |
| 869 |
pe_get_file_size, |
| 870 |
pe_get_section_count, |
| 871 |
pe_load_section, |
| 872 |
0 /* ignore pe relocations. */ |
| 873 |
}; |
| 874 |
|
| 875 |
/* On any error this frees internals. */ |
| 876 |
static int |
| 877 |
_dwarf_pe_object_access_internals_init( |
| 878 |
dwarf_pe_object_access_internals_t * internals, |
| 879 |
int fd, |
| 880 |
unsigned ftype, |
| 881 |
unsigned endian, |
| 882 |
unsigned offsetsize, |
| 883 |
size_t filesize, |
| 884 |
int *errcode) |
| 885 |
{ |
| 886 |
dwarf_pe_object_access_internals_t * intfc = internals; |
| 887 |
struct Dwarf_Obj_Access_Interface_a_s *localdoas = 0; |
| 888 |
int res = 0; |
| 889 |
|
| 890 |
/* Must malloc as _dwarf_destruct_pe_access() |
| 891 |
forces that due to other uses. */ |
| 892 |
localdoas = (struct Dwarf_Obj_Access_Interface_a_s *) |
| 893 |
malloc(sizeof(struct Dwarf_Obj_Access_Interface_a_s)); |
| 894 |
if (!localdoas) { |
| 895 |
free(internals); |
| 896 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 897 |
return DW_DLV_ERROR; |
| 898 |
} |
| 899 |
memset(localdoas,0,sizeof(struct Dwarf_Obj_Access_Interface_a_s)); |
| 900 |
intfc->pe_ident[0] = 'P'; |
| 901 |
intfc->pe_ident[1] = '1'; |
| 902 |
intfc->pe_fd = fd; |
| 903 |
intfc->pe_is_64bit = ((offsetsize==64)?TRUE:FALSE); |
| 904 |
intfc->pe_offsetsize = offsetsize; |
| 905 |
intfc->pe_pointersize = offsetsize; |
| 906 |
intfc->pe_filesize = filesize; |
| 907 |
intfc->pe_ftype = ftype; |
| 908 |
/* pe_path set by caller */ |
| 909 |
|
| 910 |
#ifdef WORDS_BIGENDIAN |
| 911 |
if (endian == DW_END_little) { |
| 912 |
intfc->pe_copy_word = _dwarf_memcpy_swap_bytes; |
| 913 |
intfc->pe_endian = DW_END_little; |
| 914 |
} else { |
| 915 |
intfc->pe_copy_word = _dwarf_memcpy_noswap_bytes; |
| 916 |
intfc->pe_endian = DW_END_big; |
| 917 |
} |
| 918 |
#else /* LITTLE ENDIAN */ |
| 919 |
if (endian == DW_END_little) { |
| 920 |
intfc->pe_copy_word = _dwarf_memcpy_noswap_bytes; |
| 921 |
intfc->pe_endian = DW_END_little; |
| 922 |
} else { |
| 923 |
intfc->pe_copy_word = _dwarf_memcpy_swap_bytes; |
| 924 |
intfc->pe_endian = DW_END_big; |
| 925 |
} |
| 926 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 927 |
res = _dwarf_load_pe_sections(intfc,errcode); |
| 928 |
if (res != DW_DLV_OK) { |
| 929 |
localdoas->ai_object = intfc; |
| 930 |
localdoas->ai_methods = 0; |
| 931 |
_dwarf_destruct_pe_access(localdoas); |
| 932 |
localdoas = 0; |
| 933 |
return res; |
| 934 |
} |
| 935 |
free(localdoas); |
| 936 |
localdoas = 0; |
| 937 |
return DW_DLV_OK; |
| 938 |
} |
| 939 |
|
| 940 |
static int |
| 941 |
_dwarf_pe_object_access_init( |
| 942 |
int fd, |
| 943 |
unsigned ftype, |
| 944 |
unsigned endian, |
| 945 |
unsigned offsetsize, |
| 946 |
size_t filesize, |
| 947 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 948 |
int *localerrnum) |
| 949 |
{ |
| 950 |
|
| 951 |
int res = 0; |
| 952 |
dwarf_pe_object_access_internals_t *internals = 0; |
| 953 |
Dwarf_Obj_Access_Interface_a *intfc = 0; |
| 954 |
|
| 955 |
internals = malloc(sizeof(dwarf_pe_object_access_internals_t)); |
| 956 |
if (!internals) { |
| 957 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 958 |
/* Impossible case, we hope. Give up. */ |
| 959 |
return DW_DLV_ERROR; |
| 960 |
} |
| 961 |
memset(internals,0,sizeof(*internals)); |
| 962 |
res = _dwarf_pe_object_access_internals_init(internals, |
| 963 |
fd, |
| 964 |
ftype, endian, offsetsize, filesize, |
| 965 |
localerrnum); |
| 966 |
if (res != DW_DLV_OK){ |
| 967 |
/* *err is already set. and the call freed internals */ |
| 968 |
return DW_DLV_ERROR; |
| 969 |
} |
| 970 |
|
| 971 |
intfc = malloc(sizeof(Dwarf_Obj_Access_Interface_a)); |
| 972 |
if (!intfc) { |
| 973 |
/* Impossible case, we hope. Give up. */ |
| 974 |
free(internals); |
| 975 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 976 |
return DW_DLV_ERROR; |
| 977 |
} |
| 978 |
/* Initialize the interface struct */ |
| 979 |
intfc->ai_object = internals; |
| 980 |
intfc->ai_methods = &pe_methods; |
| 981 |
*binary_interface = intfc; |
| 982 |
return DW_DLV_OK; |
| 983 |
} |