| 1 |
/* |
| 2 |
Copyright (c) 2019, David Anderson All rights reserved. |
| 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 |
/* This file reads the parts of an Apple mach-o object |
| 32 |
file appropriate to reading DWARF debugging data. |
| 33 |
Overview: |
| 34 |
_dwarf_macho_setup() Does all macho setup. |
| 35 |
calls _dwarf_macho_access_init() |
| 36 |
calls _dwarf_macho_object_access_internals_init() |
| 37 |
Creates internals record 'M', |
| 38 |
dwarf_macho_object_access_internals_t |
| 39 |
Sets flags/data in internals record |
| 40 |
Loads macho object data needed later. |
| 41 |
Sets methods struct to access macho object. |
| 42 |
calls _dwarf_object_init_b() Creates Dwarf_Debug, independent |
| 43 |
of any macho code. |
| 44 |
Sets internals record into dbg. |
| 45 |
---------------------- |
| 46 |
_dwarf_destruct_macho_access(). This frees |
| 47 |
the macho internals record created in |
| 48 |
_dwarf_macho_object_access_internals_init() |
| 49 |
in case of errors during setup or when |
| 50 |
dwarf_finish() is called. Works safely for |
| 51 |
partially or fully set-up macho internals record. |
| 52 |
|
| 53 |
Other than in _dwarf_macho_setup() the macho code |
| 54 |
knows nothing about Dwarf_Debug, and the rest of |
| 55 |
libdwarf knows nothing about the content of the |
| 56 |
macho internals record. |
| 57 |
|
| 58 |
*/ |
| 59 |
|
| 60 |
#include <config.h> |
| 61 |
#include <stdlib.h> /* calloc() free() malloc() */ |
| 62 |
#include <string.h> /* memcpy() memset() strcmp() strdup() */ |
| 63 |
#include <stdio.h> /* debugging printf */ |
| 64 |
|
| 65 |
#include "dwarf.h" |
| 66 |
#include "libdwarf.h" |
| 67 |
#include "libdwarf_private.h" |
| 68 |
#include "dwarf_base_types.h" |
| 69 |
#include "dwarf_safe_strcpy.h" |
| 70 |
#include "dwarf_opaque.h" |
| 71 |
#include "dwarf_error.h" /* for _dwarf_error() declaration */ |
| 72 |
#include "dwarf_reading.h" |
| 73 |
#include "dwarf_memcpy_swap.h" |
| 74 |
#include "dwarf_object_read_common.h" |
| 75 |
#include "dwarf_universal.h" |
| 76 |
#include "dwarf_machoread.h" |
| 77 |
#include "dwarf_object_detector.h" |
| 78 |
#include "dwarf_macho_loader.h" |
| 79 |
|
| 80 |
#if 0 /* dump_bytes */ |
| 81 |
static void |
| 82 |
dump_bytes(const char *msg,Dwarf_Small * start, long len) |
| 83 |
{ |
| 84 |
Dwarf_Small *end = start + len; |
| 85 |
Dwarf_Small *cur = start; |
| 86 |
int ct = 0; |
| 87 |
printf("%s (0x%lx) ",msg,(unsigned long)start); |
| 88 |
for (; cur < end; cur++) { |
| 89 |
printf("%02x", *cur); |
| 90 |
ct++; |
| 91 |
if (ct %4 == 0) { |
| 92 |
printf(" "); |
| 93 |
} |
| 94 |
} |
| 95 |
printf("\n"); |
| 96 |
} |
| 97 |
#endif /*0*/ |
| 98 |
#if 0 /* print_arch_item debugging */ |
| 99 |
static void |
| 100 |
print_arch_item(unsigned int i, |
| 101 |
struct Dwarf_Universal_Arch_s* arch) |
| 102 |
{ |
| 103 |
printf(" Universal Binary Index %u\n",i); |
| 104 |
printf(" cpu 0x%x\n",(unsigned)arch->au_cputype); |
| 105 |
printf(" cpusubt 0x%x\n",(unsigned)arch->au_cpusubtype); |
| 106 |
printf(" offset 0x%x\n",(unsigned)arch->au_offset); |
| 107 |
printf(" size 0x%x\n",(unsigned)arch->au_size); |
| 108 |
printf(" align 0x%x\n",(unsigned)arch->au_align); |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
/* MACH-O and dwarf section names */ |
| 113 |
static struct macho_sect_names_s { |
| 114 |
char const *ms_moname; |
| 115 |
char const *ms_dwname; |
| 116 |
} const SectionNames [] = { |
| 117 |
{ "", "" }, /* ELF index-0 entry */ |
| 118 |
{ "__debug_abbrev", ".debug_abbrev" }, |
| 119 |
{ "__debug_aranges", ".debug_aranges" }, |
| 120 |
{ "__debug_frame", ".debug_frame" }, |
| 121 |
{ "__debug_info", ".debug_info" }, |
| 122 |
{ "__debug_addr", ".debug_addr" }, |
| 123 |
{ "__debug_line", ".debug_line" }, |
| 124 |
{ "__debug_rnglists", ".debug_rnglists" }, |
| 125 |
{ "__debug_loclists", ".debug_loclists" }, |
| 126 |
{ "__debug_macinfo", ".debug_macinfo" }, |
| 127 |
{ "__debug_loc", ".debug_loc" }, |
| 128 |
{ "__debug_pubnames", ".debug_pubnames" }, |
| 129 |
{ "__debug_pubtypes", ".debug_pubtypes" }, |
| 130 |
{ "__debug_str", ".debug_str" }, |
| 131 |
{ "__debug_str_offs", ".debug_str_offsets" }, |
| 132 |
{ "__debug_line_str", ".debug_line_str" }, |
| 133 |
{ "__debug_ranges", ".debug_ranges" }, |
| 134 |
{ "__debug_macro", ".debug_macro" }, |
| 135 |
{ "__debug_names", ".debug_names" }, |
| 136 |
{ "__debug_gdb_scri", ".debug_gdb_scripts" } |
| 137 |
}; |
| 138 |
|
| 139 |
static int |
| 140 |
_dwarf_object_detector_universal_head_fd( |
| 141 |
int fd, |
| 142 |
Dwarf_Unsigned dw_filesize, |
| 143 |
unsigned int *dw_contentcount, |
| 144 |
Dwarf_Universal_Head * dw_head, |
| 145 |
int *errcode); |
| 146 |
|
| 147 |
static int _dwarf_macho_object_access_init( |
| 148 |
int fd, |
| 149 |
unsigned uninumber, |
| 150 |
unsigned ftype, |
| 151 |
unsigned endian, |
| 152 |
unsigned offsetsize, |
| 153 |
unsigned * universalbinary_count, |
| 154 |
Dwarf_Unsigned filesize, |
| 155 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 156 |
int *localerrnum); |
| 157 |
|
| 158 |
static Dwarf_Small macho_get_byte_order (void *obj) |
| 159 |
{ |
| 160 |
dwarf_macho_object_access_internals_t *macho = |
| 161 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 162 |
return macho->mo_endian; |
| 163 |
} |
| 164 |
|
| 165 |
static Dwarf_Small macho_get_length_size (void *obj) |
| 166 |
{ |
| 167 |
dwarf_macho_object_access_internals_t *macho = |
| 168 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 169 |
return macho->mo_offsetsize/8; |
| 170 |
} |
| 171 |
|
| 172 |
static Dwarf_Small macho_get_pointer_size (void *obj) |
| 173 |
{ |
| 174 |
dwarf_macho_object_access_internals_t *macho = |
| 175 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 176 |
return macho->mo_pointersize/8; |
| 177 |
} |
| 178 |
static Dwarf_Unsigned macho_get_file_size (void *obj) |
| 179 |
{ |
| 180 |
dwarf_macho_object_access_internals_t *macho = |
| 181 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 182 |
return macho->mo_filesize; |
| 183 |
} |
| 184 |
|
| 185 |
static Dwarf_Unsigned macho_get_section_count (void *obj) |
| 186 |
{ |
| 187 |
dwarf_macho_object_access_internals_t *macho = |
| 188 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 189 |
return macho->mo_dwarf_sectioncount; |
| 190 |
} |
| 191 |
|
| 192 |
static int macho_get_section_info (void *obj, |
| 193 |
Dwarf_Unsigned section_index, |
| 194 |
Dwarf_Obj_Access_Section_a *return_section, |
| 195 |
int *error) |
| 196 |
{ |
| 197 |
dwarf_macho_object_access_internals_t *macho = |
| 198 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 199 |
|
| 200 |
(void)error; |
| 201 |
if (section_index < macho->mo_dwarf_sectioncount) { |
| 202 |
struct generic_macho_section *sp = 0; |
| 203 |
|
| 204 |
sp = macho->mo_dwarf_sections + section_index; |
| 205 |
return_section->as_name = sp->dwarfsectname; |
| 206 |
return_section->as_type = 0; |
| 207 |
return_section->as_flags = sp->flags; |
| 208 |
return_section->as_addr = sp->addr; |
| 209 |
return_section->as_offset = sp->offset; |
| 210 |
return_section->as_size = sp->size; |
| 211 |
return_section->as_link = 0; |
| 212 |
return_section->as_info = 0; |
| 213 |
return_section->as_addralign = 0; |
| 214 |
return_section->as_entrysize = 0; |
| 215 |
return DW_DLV_OK; |
| 216 |
} |
| 217 |
return DW_DLV_NO_ENTRY; |
| 218 |
} |
| 219 |
|
| 220 |
static int |
| 221 |
macho_load_section (void *obj, Dwarf_Unsigned section_index, |
| 222 |
Dwarf_Small **return_data, int *error) |
| 223 |
{ |
| 224 |
dwarf_macho_object_access_internals_t *macho = |
| 225 |
(dwarf_macho_object_access_internals_t*)(obj); |
| 226 |
|
| 227 |
if (0 < section_index && |
| 228 |
section_index < macho->mo_dwarf_sectioncount) { |
| 229 |
int res = 0; |
| 230 |
Dwarf_Unsigned inner = macho->mo_inner_offset; |
| 231 |
|
| 232 |
struct generic_macho_section *sp = |
| 233 |
macho->mo_dwarf_sections + section_index; |
| 234 |
if (sp->loaded_data) { |
| 235 |
*return_data = sp->loaded_data; |
| 236 |
return DW_DLV_OK; |
| 237 |
} |
| 238 |
if (!sp->size) { |
| 239 |
return DW_DLV_NO_ENTRY; |
| 240 |
} |
| 241 |
if ((sp->size + sp->offset) > |
| 242 |
macho->mo_filesize) { |
| 243 |
*error = DW_DLE_FILE_TOO_SMALL; |
| 244 |
return DW_DLV_ERROR; |
| 245 |
} |
| 246 |
|
| 247 |
sp->loaded_data = malloc((size_t)sp->size); |
| 248 |
if (!sp->loaded_data) { |
| 249 |
*error = DW_DLE_ALLOC_FAIL; |
| 250 |
return DW_DLV_ERROR; |
| 251 |
} |
| 252 |
res = RRMOA(macho->mo_fd, |
| 253 |
sp->loaded_data, (inner+sp->offset), |
| 254 |
sp->size, |
| 255 |
(inner+macho->mo_filesize), error); |
| 256 |
if (res != DW_DLV_OK) { |
| 257 |
free(sp->loaded_data); |
| 258 |
sp->loaded_data = 0; |
| 259 |
return res; |
| 260 |
} |
| 261 |
*return_data = sp->loaded_data; |
| 262 |
return DW_DLV_OK; |
| 263 |
} |
| 264 |
return DW_DLV_NO_ENTRY; |
| 265 |
} |
| 266 |
|
| 267 |
static void |
| 268 |
_dwarf_destruct_macho_internals( |
| 269 |
dwarf_macho_object_access_internals_t *mp) |
| 270 |
{ |
| 271 |
Dwarf_Unsigned i = 0; |
| 272 |
|
| 273 |
if (mp->mo_destruct_close_fd) { |
| 274 |
_dwarf_closer(mp->mo_fd); |
| 275 |
mp->mo_fd = -1; |
| 276 |
} |
| 277 |
if (mp->mo_commands){ |
| 278 |
free(mp->mo_commands); |
| 279 |
mp->mo_commands = 0; |
| 280 |
} |
| 281 |
if (mp->mo_segment_commands){ |
| 282 |
free(mp->mo_segment_commands); |
| 283 |
mp->mo_segment_commands = 0; |
| 284 |
} |
| 285 |
free((char *)mp->mo_path); |
| 286 |
if (mp->mo_dwarf_sections) { |
| 287 |
struct generic_macho_section *sp = 0; |
| 288 |
|
| 289 |
sp = mp->mo_dwarf_sections; |
| 290 |
for ( i=0; i < mp->mo_dwarf_sectioncount; ++i,++sp) { |
| 291 |
if (sp->loaded_data) { |
| 292 |
free(sp->loaded_data); |
| 293 |
sp->loaded_data = 0; |
| 294 |
} |
| 295 |
} |
| 296 |
free(mp->mo_dwarf_sections); |
| 297 |
mp->mo_dwarf_sections = 0; |
| 298 |
} |
| 299 |
free(mp); |
| 300 |
return; |
| 301 |
} |
| 302 |
void |
| 303 |
_dwarf_destruct_macho_access( |
| 304 |
struct Dwarf_Obj_Access_Interface_a_s *aip) |
| 305 |
{ |
| 306 |
dwarf_macho_object_access_internals_t *mp = 0; |
| 307 |
|
| 308 |
if (!aip) { |
| 309 |
return; |
| 310 |
} |
| 311 |
mp = (dwarf_macho_object_access_internals_t *)aip->ai_object; |
| 312 |
_dwarf_destruct_macho_internals(mp); |
| 313 |
aip->ai_object = 0; |
| 314 |
free(aip); |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
/* load_macho_header32(dwarf_macho_object_access_internals_t *mfp)*/ |
| 319 |
static int |
| 320 |
load_macho_header32(dwarf_macho_object_access_internals_t *mfp, |
| 321 |
int *errcode) |
| 322 |
{ |
| 323 |
struct mach_header mh32; |
| 324 |
int res = 0; |
| 325 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 326 |
|
| 327 |
if (sizeof(mh32) > mfp->mo_filesize) { |
| 328 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 329 |
return DW_DLV_ERROR; |
| 330 |
} |
| 331 |
res = RRMOA(mfp->mo_fd, &mh32, inner, sizeof(mh32), |
| 332 |
(inner+mfp->mo_filesize), errcode); |
| 333 |
if (res != DW_DLV_OK) { |
| 334 |
return res; |
| 335 |
} |
| 336 |
/* Do not adjust endianness of magic, leave as-is. */ |
| 337 |
ASNAR(memcpy,mfp->mo_header.magic,mh32.magic); |
| 338 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.cputype,mh32.cputype); |
| 339 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.cpusubtype, |
| 340 |
mh32.cpusubtype); |
| 341 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.filetype,mh32.filetype); |
| 342 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.ncmds,mh32.ncmds); |
| 343 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.sizeofcmds, |
| 344 |
mh32.sizeofcmds); |
| 345 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.flags,mh32.flags); |
| 346 |
mfp->mo_header.reserved = 0; |
| 347 |
mfp->mo_command_count = (unsigned int)mfp->mo_header.ncmds; |
| 348 |
if (mfp->mo_command_count >= mfp->mo_filesize || |
| 349 |
mfp->mo_header.sizeofcmds >= mfp->mo_filesize || |
| 350 |
mfp->mo_command_count >= mfp->mo_header.sizeofcmds) { |
| 351 |
*errcode = DW_DLE_MACHO_CORRUPT_HEADER; |
| 352 |
return DW_DLV_ERROR; |
| 353 |
} |
| 354 |
mfp->mo_machine = mfp->mo_header.cputype; |
| 355 |
mfp->mo_flags = mfp->mo_header.flags; |
| 356 |
mfp->mo_command_start_offset = sizeof(mh32); |
| 357 |
return DW_DLV_OK; |
| 358 |
} |
| 359 |
|
| 360 |
/* load_macho_header64(dwarf_macho_object_access_internals_t *mfp) */ |
| 361 |
static int |
| 362 |
load_macho_header64(dwarf_macho_object_access_internals_t *mfp, |
| 363 |
int *errcode) |
| 364 |
{ |
| 365 |
struct mach_header_64 mh64; |
| 366 |
int res = 0; |
| 367 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 368 |
|
| 369 |
if (sizeof(mh64) > mfp->mo_filesize) { |
| 370 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 371 |
return DW_DLV_ERROR; |
| 372 |
} |
| 373 |
res = RRMOA(mfp->mo_fd, &mh64, inner, sizeof(mh64), |
| 374 |
(inner+mfp->mo_filesize), errcode); |
| 375 |
if (res != DW_DLV_OK) { |
| 376 |
return res; |
| 377 |
} |
| 378 |
/* Do not adjust endianness of magic, leave as-is. */ |
| 379 |
ASNAR(memcpy,mfp->mo_header.magic,mh64.magic); |
| 380 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.cputype,mh64.cputype); |
| 381 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.cpusubtype, |
| 382 |
mh64.cpusubtype); |
| 383 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.filetype,mh64.filetype); |
| 384 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.ncmds,mh64.ncmds); |
| 385 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.sizeofcmds, |
| 386 |
mh64.sizeofcmds); |
| 387 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.flags,mh64.flags); |
| 388 |
ASNAR(mfp->mo_copy_word,mfp->mo_header.reserved,mh64.reserved); |
| 389 |
mfp->mo_command_count = (unsigned int)mfp->mo_header.ncmds; |
| 390 |
if (mfp->mo_command_count >= mfp->mo_filesize || |
| 391 |
mfp->mo_header.sizeofcmds >= mfp->mo_filesize || |
| 392 |
mfp->mo_command_count >= mfp->mo_header.sizeofcmds) { |
| 393 |
*errcode = DW_DLE_MACHO_CORRUPT_HEADER; |
| 394 |
return DW_DLV_ERROR; |
| 395 |
} |
| 396 |
mfp->mo_machine = mfp->mo_header.cputype; |
| 397 |
mfp->mo_flags = mfp->mo_header.flags; |
| 398 |
mfp->mo_command_start_offset = sizeof(mh64); |
| 399 |
return DW_DLV_OK; |
| 400 |
} |
| 401 |
|
| 402 |
int |
| 403 |
_dwarf_load_macho_header(dwarf_macho_object_access_internals_t *mfp, |
| 404 |
int *errcode) |
| 405 |
{ |
| 406 |
int res = 0; |
| 407 |
|
| 408 |
if (mfp->mo_offsetsize == 32) { |
| 409 |
res = load_macho_header32(mfp,errcode); |
| 410 |
} else if (mfp->mo_offsetsize == 64) { |
| 411 |
res = load_macho_header64(mfp,errcode); |
| 412 |
} else { |
| 413 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 414 |
return DW_DLV_ERROR; |
| 415 |
} |
| 416 |
return res; |
| 417 |
} |
| 418 |
|
| 419 |
static int |
| 420 |
load_segment_command_content32( |
| 421 |
dwarf_macho_object_access_internals_t *mfp, |
| 422 |
struct generic_macho_command *mmp, |
| 423 |
struct generic_macho_segment_command *msp, |
| 424 |
Dwarf_Unsigned mmpindex, |
| 425 |
int *errcode) |
| 426 |
{ |
| 427 |
struct segment_command sc; |
| 428 |
int res = 0; |
| 429 |
Dwarf_Unsigned filesize = mfp->mo_filesize; |
| 430 |
Dwarf_Unsigned segoffset = mmp->offset_this_command; |
| 431 |
Dwarf_Unsigned afterseghdr = segoffset + sizeof(sc); |
| 432 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 433 |
|
| 434 |
if (segoffset > filesize || |
| 435 |
mmp->cmdsize > filesize || |
| 436 |
(mmp->cmdsize + segoffset) > filesize ) { |
| 437 |
*errcode = DW_DLE_MACH_O_SEGOFFSET_BAD; |
| 438 |
return DW_DLV_ERROR; |
| 439 |
} |
| 440 |
res = RRMOA(mfp->mo_fd, &sc, (inner+segoffset), |
| 441 |
sizeof(sc), (inner+filesize), errcode); |
| 442 |
if (res != DW_DLV_OK) { |
| 443 |
return res; |
| 444 |
} |
| 445 |
ASNAR(mfp->mo_copy_word,msp->cmd,sc.cmd); |
| 446 |
ASNAR(mfp->mo_copy_word,msp->cmdsize,sc.cmdsize); |
| 447 |
_dwarf_safe_strcpy(msp->segname, |
| 448 |
sizeof(msp->segname), |
| 449 |
sc.segname,sizeof(sc.segname)); |
| 450 |
ASNAR(mfp->mo_copy_word,msp->vmaddr,sc.vmaddr); |
| 451 |
ASNAR(mfp->mo_copy_word,msp->vmsize,sc.vmsize); |
| 452 |
ASNAR(mfp->mo_copy_word,msp->fileoff,sc.fileoff); |
| 453 |
ASNAR(mfp->mo_copy_word,msp->filesize,sc.filesize); |
| 454 |
if (msp->fileoff > mfp->mo_filesize || |
| 455 |
msp->filesize > mfp->mo_filesize) { |
| 456 |
/* corrupt */ |
| 457 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 458 |
return DW_DLV_ERROR; |
| 459 |
} |
| 460 |
if ((msp->fileoff+msp->filesize ) > filesize) { |
| 461 |
/* corrupt */ |
| 462 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 463 |
return DW_DLV_ERROR; |
| 464 |
} |
| 465 |
ASNAR(mfp->mo_copy_word,msp->maxprot,sc.maxprot); |
| 466 |
ASNAR(mfp->mo_copy_word,msp->initprot,sc.initprot); |
| 467 |
ASNAR(mfp->mo_copy_word,msp->nsects,sc.nsects); |
| 468 |
if (msp->nsects >= mfp->mo_filesize) { |
| 469 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 470 |
return DW_DLV_ERROR; |
| 471 |
} |
| 472 |
ASNAR(mfp->mo_copy_word,msp->flags,sc.flags); |
| 473 |
msp->macho_command_index = mmpindex; |
| 474 |
msp->sectionsoffset = afterseghdr; |
| 475 |
return DW_DLV_OK; |
| 476 |
} |
| 477 |
|
| 478 |
static int |
| 479 |
load_segment_command_content64( |
| 480 |
dwarf_macho_object_access_internals_t *mfp, |
| 481 |
struct generic_macho_command *mmp, |
| 482 |
struct generic_macho_segment_command *msp, |
| 483 |
Dwarf_Unsigned mmpindex,int *errcode) |
| 484 |
{ |
| 485 |
struct segment_command_64 sc; |
| 486 |
int res = 0; |
| 487 |
Dwarf_Unsigned filesize = mfp->mo_filesize; |
| 488 |
Dwarf_Unsigned segoffset = mmp->offset_this_command; |
| 489 |
Dwarf_Unsigned afterseghdr = segoffset + sizeof(sc); |
| 490 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 491 |
|
| 492 |
if (segoffset > filesize || |
| 493 |
mmp->cmdsize > filesize || |
| 494 |
(mmp->cmdsize + segoffset) > filesize ) { |
| 495 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 496 |
return DW_DLV_ERROR; |
| 497 |
} |
| 498 |
res = RRMOA(mfp->mo_fd,&sc,inner+segoffset, |
| 499 |
sizeof(sc), inner+filesize, errcode); |
| 500 |
if (res != DW_DLV_OK) { |
| 501 |
return res; |
| 502 |
} |
| 503 |
ASNAR(mfp->mo_copy_word,msp->cmd,sc.cmd); |
| 504 |
ASNAR(mfp->mo_copy_word,msp->cmdsize,sc.cmdsize); |
| 505 |
_dwarf_safe_strcpy(msp->segname,sizeof(msp->segname), |
| 506 |
sc.segname,sizeof(sc.segname)); |
| 507 |
ASNAR(mfp->mo_copy_word,msp->vmaddr,sc.vmaddr); |
| 508 |
ASNAR(mfp->mo_copy_word,msp->vmsize,sc.vmsize); |
| 509 |
ASNAR(mfp->mo_copy_word,msp->fileoff,sc.fileoff); |
| 510 |
ASNAR(mfp->mo_copy_word,msp->filesize,sc.filesize); |
| 511 |
if (msp->fileoff > filesize || |
| 512 |
msp->filesize > filesize) { |
| 513 |
/* corrupt */ |
| 514 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 515 |
return DW_DLV_ERROR; |
| 516 |
} |
| 517 |
if ((msp->fileoff+msp->filesize ) > filesize) { |
| 518 |
/* corrupt */ |
| 519 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 520 |
return DW_DLV_ERROR; |
| 521 |
} |
| 522 |
ASNAR(mfp->mo_copy_word,msp->maxprot,sc.maxprot); |
| 523 |
ASNAR(mfp->mo_copy_word,msp->initprot,sc.initprot); |
| 524 |
ASNAR(mfp->mo_copy_word,msp->nsects,sc.nsects); |
| 525 |
if (msp->nsects >= mfp->mo_filesize) { |
| 526 |
*errcode = DW_DLE_MACHO_CORRUPT_COMMAND; |
| 527 |
return DW_DLV_ERROR; |
| 528 |
} |
| 529 |
ASNAR(mfp->mo_copy_word,msp->flags,sc.flags); |
| 530 |
msp->macho_command_index = mmpindex; |
| 531 |
msp->sectionsoffset = afterseghdr; |
| 532 |
return DW_DLV_OK; |
| 533 |
} |
| 534 |
|
| 535 |
static int |
| 536 |
_dwarf_macho_load_segment_commands( |
| 537 |
dwarf_macho_object_access_internals_t *mfp,int *errcode) |
| 538 |
{ |
| 539 |
Dwarf_Unsigned i = 0; |
| 540 |
struct generic_macho_command *mmp = 0; |
| 541 |
struct generic_macho_segment_command *msp = 0; |
| 542 |
|
| 543 |
if (mfp->mo_segment_count < 1) { |
| 544 |
return DW_DLV_OK; |
| 545 |
} |
| 546 |
mfp->mo_segment_commands = |
| 547 |
(struct generic_macho_segment_command *) |
| 548 |
calloc((size_t)mfp->mo_segment_count, |
| 549 |
sizeof(struct generic_macho_segment_command)); |
| 550 |
if (!mfp->mo_segment_commands) { |
| 551 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 552 |
return DW_DLV_ERROR; |
| 553 |
} |
| 554 |
|
| 555 |
mmp = mfp->mo_commands; |
| 556 |
msp = mfp->mo_segment_commands; |
| 557 |
for (i = 0 ; i < mfp->mo_command_count; ++i,++mmp) { |
| 558 |
unsigned cmd = (unsigned)mmp->cmd; |
| 559 |
int res = 0; |
| 560 |
|
| 561 |
if (cmd == LC_SEGMENT) { |
| 562 |
res = load_segment_command_content32(mfp,mmp,msp, |
| 563 |
i,errcode); |
| 564 |
++msp; |
| 565 |
} else if (cmd == LC_SEGMENT_64) { |
| 566 |
res = load_segment_command_content64(mfp,mmp,msp, |
| 567 |
i,errcode); |
| 568 |
++msp; |
| 569 |
} else { /* fall through, not a command of interest */ } |
| 570 |
if (res != DW_DLV_OK) { |
| 571 |
free(mfp->mo_segment_commands); |
| 572 |
mfp->mo_segment_commands = 0; |
| 573 |
return res; |
| 574 |
} |
| 575 |
} |
| 576 |
return DW_DLV_OK; |
| 577 |
} |
| 578 |
|
| 579 |
static int |
| 580 |
_dwarf_macho_load_dwarf_section_details32( |
| 581 |
dwarf_macho_object_access_internals_t *mfp, |
| 582 |
struct generic_macho_segment_command *segp, |
| 583 |
Dwarf_Unsigned segi, int *errcode) |
| 584 |
{ |
| 585 |
Dwarf_Unsigned seci = 0; |
| 586 |
Dwarf_Unsigned seccount = segp->nsects; |
| 587 |
Dwarf_Unsigned secalloc = seccount+1; |
| 588 |
Dwarf_Unsigned curoff = segp->sectionsoffset; |
| 589 |
Dwarf_Unsigned shdrlen = sizeof(struct section); |
| 590 |
|
| 591 |
struct generic_macho_section *secs = 0; |
| 592 |
|
| 593 |
secs = (struct generic_macho_section *)calloc( |
| 594 |
(size_t)secalloc, |
| 595 |
sizeof(struct generic_macho_section)); |
| 596 |
if (!secs) { |
| 597 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 598 |
return DW_DLV_OK; |
| 599 |
} |
| 600 |
mfp->mo_dwarf_sections = secs; |
| 601 |
mfp->mo_dwarf_sectioncount = secalloc; |
| 602 |
if ((curoff > mfp->mo_filesize) || |
| 603 |
(seccount > mfp->mo_filesize) || |
| 604 |
(curoff+(seccount*sizeof(struct section)) > |
| 605 |
mfp->mo_filesize)) { |
| 606 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 607 |
return DW_DLV_ERROR; |
| 608 |
} |
| 609 |
secs->offset_of_sec_rec = curoff; |
| 610 |
/* Leave 0 section all zeros except our offset, |
| 611 |
elf-like in a sense */ |
| 612 |
secs->dwarfsectname = ""; |
| 613 |
++secs; |
| 614 |
seci = 1; |
| 615 |
for (; seci < secalloc; ++seci,++secs,curoff += shdrlen ) { |
| 616 |
struct section mosec; |
| 617 |
int res = 0; |
| 618 |
Dwarf_Unsigned endoffset = 0; |
| 619 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 620 |
|
| 621 |
endoffset = curoff + sizeof(mosec); |
| 622 |
if (curoff >= mfp->mo_filesize || |
| 623 |
endoffset > mfp->mo_filesize) { |
| 624 |
*errcode = DW_DLE_MACHO_CORRUPT_SECTIONDETAILS; |
| 625 |
return DW_DLV_ERROR; |
| 626 |
} |
| 627 |
res = RRMOA(mfp->mo_fd, &mosec, |
| 628 |
inner+curoff, sizeof(mosec), |
| 629 |
inner+mfp->mo_filesize, errcode); |
| 630 |
if (res != DW_DLV_OK) { |
| 631 |
return res; |
| 632 |
} |
| 633 |
_dwarf_safe_strcpy(secs->sectname, |
| 634 |
sizeof(secs->sectname), |
| 635 |
mosec.sectname,sizeof(mosec.sectname)); |
| 636 |
_dwarf_safe_strcpy(secs->segname, |
| 637 |
sizeof(secs->segname), |
| 638 |
mosec.segname,sizeof(mosec.segname)); |
| 639 |
ASNAR(mfp->mo_copy_word,secs->addr,mosec.addr); |
| 640 |
ASNAR(mfp->mo_copy_word,secs->size,mosec.size); |
| 641 |
ASNAR(mfp->mo_copy_word,secs->offset,mosec.offset); |
| 642 |
ASNAR(mfp->mo_copy_word,secs->align,mosec.align); |
| 643 |
ASNAR(mfp->mo_copy_word,secs->reloff,mosec.reloff); |
| 644 |
ASNAR(mfp->mo_copy_word,secs->nreloc,mosec.nreloc); |
| 645 |
ASNAR(mfp->mo_copy_word,secs->flags,mosec.flags); |
| 646 |
if (secs->offset > mfp->mo_filesize || |
| 647 |
secs->size > mfp->mo_filesize || |
| 648 |
(secs->offset+secs->size) > mfp->mo_filesize) { |
| 649 |
*errcode = DW_DLE_MACHO_CORRUPT_SECTIONDETAILS; |
| 650 |
return DW_DLV_ERROR; |
| 651 |
} |
| 652 |
secs->reserved1 = 0; |
| 653 |
secs->reserved2 = 0; |
| 654 |
secs->reserved3 = 0; |
| 655 |
secs->generic_segment_num = segi; |
| 656 |
secs->offset_of_sec_rec = curoff; |
| 657 |
} |
| 658 |
return DW_DLV_OK; |
| 659 |
} |
| 660 |
static int |
| 661 |
_dwarf_macho_load_dwarf_section_details64( |
| 662 |
dwarf_macho_object_access_internals_t *mfp, |
| 663 |
struct generic_macho_segment_command *segp, |
| 664 |
Dwarf_Unsigned segi, |
| 665 |
int *errcode) |
| 666 |
{ |
| 667 |
Dwarf_Unsigned seci = 0; |
| 668 |
Dwarf_Unsigned seccount = segp->nsects; |
| 669 |
Dwarf_Unsigned secalloc = seccount+1; |
| 670 |
Dwarf_Unsigned curoff = segp->sectionsoffset; |
| 671 |
Dwarf_Unsigned shdrlen = sizeof(struct section_64); |
| 672 |
struct generic_macho_section *secs = 0; |
| 673 |
|
| 674 |
secs = (struct generic_macho_section *)calloc( |
| 675 |
(size_t)secalloc, |
| 676 |
sizeof(struct generic_macho_section)); |
| 677 |
if (!secs) { |
| 678 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 679 |
return DW_DLV_ERROR; |
| 680 |
} |
| 681 |
mfp->mo_dwarf_sections = secs; |
| 682 |
mfp->mo_dwarf_sectioncount = secalloc; |
| 683 |
secs->offset_of_sec_rec = curoff; |
| 684 |
/* Leave 0 section all zeros except our offset, |
| 685 |
elf-like in a sense */ |
| 686 |
secs->dwarfsectname = ""; |
| 687 |
++secs; |
| 688 |
if ((curoff > mfp->mo_filesize) || |
| 689 |
(seccount > mfp->mo_filesize) || |
| 690 |
(curoff+(seccount*sizeof(struct section_64)) > |
| 691 |
mfp->mo_filesize)) { |
| 692 |
*errcode = DW_DLE_FILE_TOO_SMALL; |
| 693 |
return DW_DLV_ERROR; |
| 694 |
} |
| 695 |
seci = 1; |
| 696 |
for (; seci < secalloc; ++seci,++secs,curoff += shdrlen ) { |
| 697 |
int res = 0; |
| 698 |
struct section_64 mosec; |
| 699 |
Dwarf_Unsigned endoffset = 0; |
| 700 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 701 |
|
| 702 |
endoffset = curoff + sizeof(mosec); |
| 703 |
if (curoff >= mfp->mo_filesize || |
| 704 |
endoffset > mfp->mo_filesize) { |
| 705 |
*errcode = DW_DLE_MACHO_CORRUPT_SECTIONDETAILS; |
| 706 |
return DW_DLV_ERROR; |
| 707 |
} |
| 708 |
|
| 709 |
res = RRMOA(mfp->mo_fd, &mosec, |
| 710 |
inner+curoff, sizeof(mosec), |
| 711 |
inner+mfp->mo_filesize, errcode); |
| 712 |
if (res != DW_DLV_OK) { |
| 713 |
return res; |
| 714 |
} |
| 715 |
_dwarf_safe_strcpy(secs->sectname, |
| 716 |
sizeof(secs->sectname), |
| 717 |
mosec.sectname,sizeof(mosec.sectname)); |
| 718 |
_dwarf_safe_strcpy(secs->segname, |
| 719 |
sizeof(secs->segname), |
| 720 |
mosec.segname,sizeof(mosec.segname)); |
| 721 |
ASNAR(mfp->mo_copy_word,secs->addr,mosec.addr); |
| 722 |
ASNAR(mfp->mo_copy_word,secs->size,mosec.size); |
| 723 |
ASNAR(mfp->mo_copy_word,secs->offset,mosec.offset); |
| 724 |
ASNAR(mfp->mo_copy_word,secs->align,mosec.align); |
| 725 |
ASNAR(mfp->mo_copy_word,secs->reloff,mosec.reloff); |
| 726 |
ASNAR(mfp->mo_copy_word,secs->nreloc,mosec.nreloc); |
| 727 |
ASNAR(mfp->mo_copy_word,secs->flags,mosec.flags); |
| 728 |
if (secs->offset > mfp->mo_filesize || |
| 729 |
secs->size > mfp->mo_filesize || |
| 730 |
(secs->offset+secs->size) > mfp->mo_filesize) { |
| 731 |
*errcode = DW_DLE_MACHO_CORRUPT_SECTIONDETAILS; |
| 732 |
return DW_DLV_ERROR; |
| 733 |
} |
| 734 |
secs->reserved1 = 0; |
| 735 |
secs->reserved2 = 0; |
| 736 |
secs->reserved3 = 0; |
| 737 |
secs->offset_of_sec_rec = curoff; |
| 738 |
secs->generic_segment_num = segi; |
| 739 |
} |
| 740 |
return DW_DLV_OK; |
| 741 |
} |
| 742 |
|
| 743 |
static int |
| 744 |
_dwarf_macho_load_dwarf_section_details( |
| 745 |
dwarf_macho_object_access_internals_t *mfp, |
| 746 |
struct generic_macho_segment_command *segp, |
| 747 |
Dwarf_Unsigned segi,int *errcode) |
| 748 |
{ |
| 749 |
int res = 0; |
| 750 |
|
| 751 |
if (mfp->mo_offsetsize == 32) { |
| 752 |
res = _dwarf_macho_load_dwarf_section_details32(mfp, |
| 753 |
segp,segi,errcode); |
| 754 |
} else if (mfp->mo_offsetsize == 64) { |
| 755 |
res = _dwarf_macho_load_dwarf_section_details64(mfp, |
| 756 |
segp,segi,errcode); |
| 757 |
} else { |
| 758 |
*errcode = DW_DLE_OFFSET_SIZE; |
| 759 |
return DW_DLV_ERROR; |
| 760 |
} |
| 761 |
return res; |
| 762 |
} |
| 763 |
|
| 764 |
static int |
| 765 |
_dwarf_macho_load_dwarf_sections( |
| 766 |
dwarf_macho_object_access_internals_t *mfp,int *errcode) |
| 767 |
{ |
| 768 |
Dwarf_Unsigned segi = 0; |
| 769 |
Dwarf_Unsigned ftype = mfp->mo_header.filetype; |
| 770 |
|
| 771 |
struct generic_macho_segment_command *segp = |
| 772 |
mfp->mo_segment_commands; |
| 773 |
if (ftype != MH_DSYM && |
| 774 |
ftype != MH_OBJECT) { |
| 775 |
/* We do not think it can have DWARF */ |
| 776 |
return DW_DLV_OK; |
| 777 |
} |
| 778 |
for ( ; segi < mfp->mo_segment_count; ++segi,++segp) { |
| 779 |
int res = 0; |
| 780 |
|
| 781 |
switch (ftype) { |
| 782 |
case MH_DSYM: { |
| 783 |
if (strcmp(segp->segname,"__DWARF")) { |
| 784 |
/* No DWARF in this segment */ |
| 785 |
continue; |
| 786 |
} |
| 787 |
} |
| 788 |
/* will have DWARF */ |
| 789 |
break; |
| 790 |
case MH_OBJECT: |
| 791 |
/* Likely has DWARF */ |
| 792 |
break; |
| 793 |
default: |
| 794 |
/* We do not think it can have DWARF */ |
| 795 |
continue; |
| 796 |
} |
| 797 |
res = _dwarf_macho_load_dwarf_section_details(mfp, |
| 798 |
segp,segi,errcode); |
| 799 |
return res; |
| 800 |
} |
| 801 |
return DW_DLV_OK; |
| 802 |
} |
| 803 |
|
| 804 |
/* Works the same, 32 or 64 bit */ |
| 805 |
int |
| 806 |
_dwarf_load_macho_commands( |
| 807 |
dwarf_macho_object_access_internals_t *mfp,int *errcode) |
| 808 |
{ |
| 809 |
Dwarf_Unsigned cmdi = 0; |
| 810 |
Dwarf_Unsigned curoff = mfp->mo_command_start_offset; |
| 811 |
struct load_command mc; |
| 812 |
struct generic_macho_command *mcp = 0; |
| 813 |
unsigned segment_command_count = 0; |
| 814 |
int res = 0; |
| 815 |
Dwarf_Unsigned inner = mfp->mo_inner_offset; |
| 816 |
|
| 817 |
if (mfp->mo_command_count >= mfp->mo_filesize) { |
| 818 |
/* corrupt object. */ |
| 819 |
*errcode = DW_DLE_MACH_O_SEGOFFSET_BAD; |
| 820 |
return DW_DLV_ERROR; |
| 821 |
} |
| 822 |
if ((curoff + mfp->mo_command_count * sizeof(mc)) >= |
| 823 |
mfp->mo_filesize) { |
| 824 |
/* corrupt object. */ |
| 825 |
*errcode = DW_DLE_MACH_O_SEGOFFSET_BAD; |
| 826 |
return DW_DLV_ERROR; |
| 827 |
} |
| 828 |
|
| 829 |
mfp->mo_commands = (struct generic_macho_command *) calloc( |
| 830 |
mfp->mo_command_count,sizeof(struct generic_macho_command)); |
| 831 |
if (!mfp->mo_commands) { |
| 832 |
/* out of memory */ |
| 833 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 834 |
return DW_DLV_ERROR; |
| 835 |
} |
| 836 |
mcp = mfp->mo_commands; |
| 837 |
for ( ; cmdi < mfp->mo_header.ncmds; ++cmdi,++mcp ) { |
| 838 |
res = RRMOA(mfp->mo_fd, &mc, |
| 839 |
inner+curoff, sizeof(mc), |
| 840 |
inner+mfp->mo_filesize, errcode); |
| 841 |
if (res != DW_DLV_OK) { |
| 842 |
free(mfp->mo_commands); |
| 843 |
mfp->mo_commands = 0; |
| 844 |
return res; |
| 845 |
} |
| 846 |
ASNAR(mfp->mo_copy_word,mcp->cmd,mc.cmd); |
| 847 |
ASNAR(mfp->mo_copy_word,mcp->cmdsize,mc.cmdsize); |
| 848 |
mcp->offset_this_command = curoff; |
| 849 |
curoff += mcp->cmdsize; |
| 850 |
if (mcp->cmdsize > mfp->mo_filesize || |
| 851 |
curoff > mfp->mo_filesize) { |
| 852 |
/* corrupt object */ |
| 853 |
free(mfp->mo_commands); |
| 854 |
mfp->mo_commands = 0; |
| 855 |
*errcode = DW_DLE_FILE_OFFSET_BAD; |
| 856 |
return DW_DLV_ERROR; |
| 857 |
} |
| 858 |
if (mcp->cmd == LC_SEGMENT || mcp->cmd == LC_SEGMENT_64) { |
| 859 |
segment_command_count++; |
| 860 |
} |
| 861 |
} |
| 862 |
mfp->mo_segment_count = segment_command_count; |
| 863 |
res = _dwarf_macho_load_segment_commands(mfp,errcode); |
| 864 |
if (res != DW_DLV_OK) { |
| 865 |
free(mfp->mo_commands); |
| 866 |
mfp->mo_commands = 0; |
| 867 |
return res; |
| 868 |
} |
| 869 |
res = _dwarf_macho_load_dwarf_sections(mfp,errcode); |
| 870 |
if (res != DW_DLV_OK) { |
| 871 |
free(mfp->mo_commands); |
| 872 |
mfp->mo_commands = 0; |
| 873 |
} |
| 874 |
return res; |
| 875 |
} |
| 876 |
int |
| 877 |
_dwarf_macho_setup(int fd, |
| 878 |
char *true_path, |
| 879 |
unsigned universalnumber, |
| 880 |
unsigned ftype, |
| 881 |
unsigned endian, |
| 882 |
unsigned offsetsize, |
| 883 |
Dwarf_Unsigned filesize, |
| 884 |
unsigned groupnumber, |
| 885 |
Dwarf_Handler errhand, |
| 886 |
Dwarf_Ptr errarg, |
| 887 |
Dwarf_Debug *dbg,Dwarf_Error *error) |
| 888 |
{ |
| 889 |
Dwarf_Obj_Access_Interface_a *binary_interface = 0; |
| 890 |
dwarf_macho_object_access_internals_t *intfc = 0; |
| 891 |
int res = DW_DLV_OK; |
| 892 |
int localerrnum = 0; |
| 893 |
unsigned universalbinary_count = 0; |
| 894 |
|
| 895 |
res = _dwarf_macho_object_access_init( |
| 896 |
fd, |
| 897 |
universalnumber, |
| 898 |
ftype,endian,offsetsize, |
| 899 |
&universalbinary_count, |
| 900 |
filesize, |
| 901 |
&binary_interface, |
| 902 |
&localerrnum); |
| 903 |
if (res != DW_DLV_OK) { |
| 904 |
if (res == DW_DLV_NO_ENTRY) { |
| 905 |
return res; |
| 906 |
} |
| 907 |
_dwarf_error(NULL, error, localerrnum); |
| 908 |
return DW_DLV_ERROR; |
| 909 |
} |
| 910 |
/* allocates and initializes Dwarf_Debug, |
| 911 |
generic code */ |
| 912 |
res = dwarf_object_init_b(binary_interface, errhand, errarg, |
| 913 |
groupnumber, dbg, error); |
| 914 |
if (res != DW_DLV_OK){ |
| 915 |
_dwarf_destruct_macho_access(binary_interface); |
| 916 |
return res; |
| 917 |
} |
| 918 |
intfc = binary_interface->ai_object; |
| 919 |
intfc->mo_path = strdup(true_path); |
| 920 |
(*dbg)->de_obj_flags = intfc->mo_flags; |
| 921 |
(*dbg)->de_obj_machine = intfc->mo_machine; |
| 922 |
(*dbg)->de_universalbinary_index = universalnumber; |
| 923 |
(*dbg)->de_universalbinary_count = universalbinary_count; |
| 924 |
return res; |
| 925 |
} |
| 926 |
|
| 927 |
/* Initialize the methods needed to read Macho object files. */ |
| 928 |
static Dwarf_Obj_Access_Methods_a const macho_methods = { |
| 929 |
macho_get_section_info, |
| 930 |
macho_get_byte_order, |
| 931 |
macho_get_length_size, |
| 932 |
macho_get_pointer_size, |
| 933 |
macho_get_file_size, |
| 934 |
macho_get_section_count, |
| 935 |
macho_load_section, |
| 936 |
/* We do not do macho relocations. |
| 937 |
dsym files do not require it. */ |
| 938 |
NULL |
| 939 |
}; |
| 940 |
|
| 941 |
/* Reads universal binary headers, gets to |
| 942 |
the chosen inner binary, and returns the |
| 943 |
values from the inner binary. |
| 944 |
The filesize being that of the inner binary, |
| 945 |
and the fileoffset being the offset of the inner |
| 946 |
binary (so by definition > 0); |
| 947 |
*/ |
| 948 |
|
| 949 |
static int |
| 950 |
_dwarf_macho_inner_object_fd(int fd, |
| 951 |
unsigned int uninumber, |
| 952 |
Dwarf_Unsigned outer_filesize, |
| 953 |
unsigned int *ftype, |
| 954 |
unsigned int *unibinarycount, |
| 955 |
unsigned int *endian, |
| 956 |
unsigned int *offsetsize, |
| 957 |
Dwarf_Unsigned *fileoffset, |
| 958 |
Dwarf_Unsigned *filesize, |
| 959 |
int *errcode) |
| 960 |
{ |
| 961 |
int res = 0; |
| 962 |
Dwarf_Universal_Head head = 0; |
| 963 |
Dwarf_Unsigned innerbase = 0; |
| 964 |
Dwarf_Unsigned innersize = 0; |
| 965 |
|
| 966 |
res = _dwarf_object_detector_universal_head_fd( |
| 967 |
fd, outer_filesize, unibinarycount, |
| 968 |
&head, errcode); |
| 969 |
if (res != DW_DLV_OK) { |
| 970 |
return res; |
| 971 |
} |
| 972 |
if (uninumber >= *unibinarycount) { |
| 973 |
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR; |
| 974 |
_dwarf_dealloc_universal_head(head); |
| 975 |
return DW_DLV_ERROR; |
| 976 |
} |
| 977 |
/* Now find the precise details of uninumber |
| 978 |
instance we want */ |
| 979 |
|
| 980 |
innerbase = head->au_arches[uninumber].au_offset; |
| 981 |
innersize = head->au_arches[uninumber].au_size; |
| 982 |
if (innersize >= outer_filesize || |
| 983 |
innerbase >= outer_filesize) { |
| 984 |
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR; |
| 985 |
_dwarf_dealloc_universal_head(head); |
| 986 |
return DW_DLV_ERROR; |
| 987 |
} |
| 988 |
/* Now access inner to return its specs */ |
| 989 |
{ |
| 990 |
/* But ignore the size this returns! |
| 991 |
we determined that above. the following call |
| 992 |
does not get the inner size, we got that |
| 993 |
just above here! */ |
| 994 |
Dwarf_Unsigned fake_size = 0; |
| 995 |
|
| 996 |
res = _dwarf_object_detector_fd_a(fd, |
| 997 |
ftype,endian,offsetsize,innerbase,&fake_size, |
| 998 |
errcode); |
| 999 |
if (res != DW_DLV_OK) { |
| 1000 |
_dwarf_dealloc_universal_head(head); |
| 1001 |
return res; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
*fileoffset = innerbase; |
| 1005 |
*filesize = innersize; |
| 1006 |
_dwarf_dealloc_universal_head(head); |
| 1007 |
return DW_DLV_OK; |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* On any error this frees internals argument. */ |
| 1011 |
static int |
| 1012 |
_dwarf_macho_object_access_internals_init( |
| 1013 |
dwarf_macho_object_access_internals_t * internals, |
| 1014 |
int fd, |
| 1015 |
unsigned uninumber, |
| 1016 |
unsigned ftype, |
| 1017 |
unsigned endian, |
| 1018 |
unsigned offsetsize, |
| 1019 |
unsigned *unibinarycount, |
| 1020 |
Dwarf_Unsigned filesize, |
| 1021 |
int *errcode) |
| 1022 |
{ |
| 1023 |
Dwarf_Unsigned i = 0; |
| 1024 |
struct generic_macho_section *sp = 0; |
| 1025 |
int res = 0; |
| 1026 |
unsigned int ftypei = ftype; |
| 1027 |
unsigned int endiani = endian; |
| 1028 |
unsigned int offsetsizei = offsetsize; |
| 1029 |
Dwarf_Unsigned filesizei = filesize; |
| 1030 |
Dwarf_Unsigned fileoffseti = 0; |
| 1031 |
unsigned int unibinarycounti = 0; |
| 1032 |
|
| 1033 |
if (ftype == DW_FTYPE_APPLEUNIVERSAL) { |
| 1034 |
res = _dwarf_macho_inner_object_fd(fd, |
| 1035 |
uninumber, |
| 1036 |
filesize, |
| 1037 |
&ftypei,&unibinarycounti,&endiani, |
| 1038 |
&offsetsizei,&fileoffseti,&filesizei,errcode); |
| 1039 |
if (res != DW_DLV_OK) { |
| 1040 |
if (res == DW_DLV_ERROR) { |
| 1041 |
} |
| 1042 |
return res; |
| 1043 |
} |
| 1044 |
*unibinarycount = unibinarycounti; |
| 1045 |
endian = endiani; |
| 1046 |
} |
| 1047 |
|
| 1048 |
internals->mo_ident[0] = 'M'; |
| 1049 |
internals->mo_ident[1] = '1'; |
| 1050 |
internals->mo_fd = fd; |
| 1051 |
internals->mo_offsetsize = offsetsizei; |
| 1052 |
internals->mo_pointersize = offsetsizei; |
| 1053 |
internals->mo_inner_offset = fileoffseti; |
| 1054 |
internals->mo_filesize = filesizei; |
| 1055 |
internals->mo_ftype = ftypei; |
| 1056 |
internals->mo_uninumber = uninumber; |
| 1057 |
internals->mo_universal_count = unibinarycounti; |
| 1058 |
|
| 1059 |
#ifdef WORDS_BIGENDIAN |
| 1060 |
if (endian == DW_END_little ) { |
| 1061 |
internals->mo_copy_word = _dwarf_memcpy_swap_bytes; |
| 1062 |
internals->mo_endian = DW_END_little; |
| 1063 |
} else { |
| 1064 |
internals->mo_copy_word = _dwarf_memcpy_noswap_bytes; |
| 1065 |
internals->mo_endian = DW_END_big; |
| 1066 |
} |
| 1067 |
#else /* LITTLE ENDIAN */ |
| 1068 |
if (endian == DW_END_little ) { |
| 1069 |
internals->mo_copy_word = _dwarf_memcpy_noswap_bytes; |
| 1070 |
internals->mo_endian = DW_END_little; |
| 1071 |
} else { |
| 1072 |
internals->mo_copy_word = _dwarf_memcpy_swap_bytes; |
| 1073 |
internals->mo_endian = DW_END_big; |
| 1074 |
} |
| 1075 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 1076 |
res = _dwarf_load_macho_header(internals,errcode); |
| 1077 |
if (res != DW_DLV_OK) { |
| 1078 |
return res; |
| 1079 |
} |
| 1080 |
/* Load sections */ |
| 1081 |
res = _dwarf_load_macho_commands(internals,errcode); |
| 1082 |
if (res != DW_DLV_OK) { |
| 1083 |
return res; |
| 1084 |
} |
| 1085 |
if (internals->mo_dwarf_sections) { |
| 1086 |
sp = internals->mo_dwarf_sections+1; |
| 1087 |
} else { |
| 1088 |
/* There are no dwarf sections, |
| 1089 |
count better be zero. */ |
| 1090 |
if (internals->mo_dwarf_sectioncount) { |
| 1091 |
*errcode = DW_DLE_MACHO_CORRUPT_HEADER; |
| 1092 |
return DW_DLV_ERROR; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
for (i = 1; i < internals->mo_dwarf_sectioncount ; ++i,++sp) { |
| 1096 |
int j = 1; |
| 1097 |
int lim = sizeof(SectionNames)/sizeof(SectionNames[0]); |
| 1098 |
sp->dwarfsectname = ""; |
| 1099 |
for ( ; j < lim; ++j) { |
| 1100 |
if (!strcmp(sp->sectname,SectionNames[j].ms_moname)) { |
| 1101 |
sp->dwarfsectname = SectionNames[j].ms_dwname; |
| 1102 |
break; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
if (sp->dwarfsectname[0] == 0) { |
| 1106 |
/* if not matched, keep the apple section name */ |
| 1107 |
sp->dwarfsectname = sp->sectname; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
return DW_DLV_OK; |
| 1111 |
} |
| 1112 |
|
| 1113 |
static int |
| 1114 |
_dwarf_macho_object_access_init( |
| 1115 |
int fd, |
| 1116 |
unsigned uninumber, |
| 1117 |
unsigned ftype, |
| 1118 |
unsigned endian, |
| 1119 |
unsigned offsetsize, |
| 1120 |
unsigned * universalbinary_count, |
| 1121 |
Dwarf_Unsigned filesize, |
| 1122 |
Dwarf_Obj_Access_Interface_a **binary_interface, |
| 1123 |
int *localerrnum) |
| 1124 |
{ |
| 1125 |
int res = 0; |
| 1126 |
dwarf_macho_object_access_internals_t *internals = 0; |
| 1127 |
Dwarf_Obj_Access_Interface_a *intfc = 0; |
| 1128 |
|
| 1129 |
internals = malloc( |
| 1130 |
sizeof(dwarf_macho_object_access_internals_t)); |
| 1131 |
if (!internals) { |
| 1132 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 1133 |
/* Impossible case, we hope. Give up. */ |
| 1134 |
return DW_DLV_ERROR; |
| 1135 |
} |
| 1136 |
memset(internals,0,sizeof(*internals)); |
| 1137 |
res = _dwarf_macho_object_access_internals_init(internals, |
| 1138 |
fd, |
| 1139 |
uninumber, |
| 1140 |
ftype, endian, offsetsize, |
| 1141 |
universalbinary_count, |
| 1142 |
filesize, |
| 1143 |
localerrnum); |
| 1144 |
if (res != DW_DLV_OK){ |
| 1145 |
_dwarf_destruct_macho_internals(internals); |
| 1146 |
return DW_DLV_ERROR; |
| 1147 |
} |
| 1148 |
intfc = malloc(sizeof(Dwarf_Obj_Access_Interface_a)); |
| 1149 |
if (!intfc) { |
| 1150 |
/* Impossible case, we hope. Give up. */ |
| 1151 |
_dwarf_destruct_macho_internals(internals); |
| 1152 |
*localerrnum = DW_DLE_ALLOC_FAIL; |
| 1153 |
return DW_DLV_ERROR; |
| 1154 |
} |
| 1155 |
/* Initialize the interface struct */ |
| 1156 |
intfc->ai_object = internals; |
| 1157 |
intfc->ai_methods = &macho_methods; |
| 1158 |
*binary_interface = intfc; |
| 1159 |
return DW_DLV_OK; |
| 1160 |
} |
| 1161 |
|
| 1162 |
static unsigned long |
| 1163 |
magic_copy(unsigned char *d, unsigned len) |
| 1164 |
{ |
| 1165 |
unsigned i = 0; |
| 1166 |
unsigned long v = 0; |
| 1167 |
|
| 1168 |
v = d[0]; |
| 1169 |
for (i = 1 ; i < len; ++i) { |
| 1170 |
v <<= 8; |
| 1171 |
v |= d[i]; |
| 1172 |
} |
| 1173 |
return v; |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* fa points to fat arch in object-file format |
| 1177 |
duhd points to space to fill in (in au_rches) data |
| 1178 |
for each macho object in the fat object. */ |
| 1179 |
static int |
| 1180 |
fill_in_uni_arch_32( |
| 1181 |
struct fat_arch * fa, |
| 1182 |
struct Dwarf_Universal_Head_s *duhd, |
| 1183 |
void (*word_swap) (void *, const void *, unsigned long), |
| 1184 |
int *errcode) |
| 1185 |
{ |
| 1186 |
Dwarf_Unsigned i = 0; |
| 1187 |
struct Dwarf_Universal_Arch_s * dua = 0; |
| 1188 |
|
| 1189 |
dua = duhd->au_arches; |
| 1190 |
for ( ; i < duhd->au_count; ++i,++fa,++dua) { |
| 1191 |
ASNAR(word_swap,dua->au_cputype,fa->cputype); |
| 1192 |
ASNAR(word_swap,dua->au_cpusubtype,fa->cpusubtype); |
| 1193 |
ASNAR(word_swap,dua->au_offset,fa->offset); |
| 1194 |
if (dua->au_offset >= duhd->au_filesize) { |
| 1195 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1196 |
return DW_DLV_ERROR; |
| 1197 |
} |
| 1198 |
ASNAR(word_swap,dua->au_size,fa->size); |
| 1199 |
if (dua->au_size >= duhd->au_filesize) { |
| 1200 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1201 |
return DW_DLV_ERROR; |
| 1202 |
} |
| 1203 |
if ((dua->au_size+dua->au_offset) > duhd->au_filesize) { |
| 1204 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1205 |
return DW_DLV_ERROR; |
| 1206 |
} |
| 1207 |
|
| 1208 |
ASNAR(word_swap,dua->au_align,fa->align); |
| 1209 |
if (dua->au_align >= 32) { |
| 1210 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1211 |
return DW_DLV_ERROR; |
| 1212 |
} |
| 1213 |
dua->au_reserved = 0; |
| 1214 |
} |
| 1215 |
return DW_DLV_OK; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int |
| 1219 |
fill_in_uni_arch_64( |
| 1220 |
struct fat_arch_64 * fa, |
| 1221 |
struct Dwarf_Universal_Head_s *duhd, |
| 1222 |
void (*word_swap) (void *, const void *, unsigned long), |
| 1223 |
int *errcode) |
| 1224 |
{ |
| 1225 |
Dwarf_Unsigned i = 0; |
| 1226 |
struct Dwarf_Universal_Arch_s * dua = 0; |
| 1227 |
|
| 1228 |
dua = duhd->au_arches; |
| 1229 |
for ( ; i < duhd->au_count; ++i,++fa,++dua) { |
| 1230 |
ASNAR(word_swap,dua->au_cputype,fa->cputype); |
| 1231 |
ASNAR(word_swap,dua->au_cpusubtype,fa->cpusubtype); |
| 1232 |
ASNAR(word_swap,dua->au_offset,fa->offset); |
| 1233 |
if (dua->au_offset >= duhd->au_filesize) { |
| 1234 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1235 |
return DW_DLV_ERROR; |
| 1236 |
} |
| 1237 |
ASNAR(word_swap,dua->au_size,fa->size); |
| 1238 |
if (dua->au_size >= duhd->au_filesize) { |
| 1239 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1240 |
return DW_DLV_ERROR; |
| 1241 |
} |
| 1242 |
if ((dua->au_size+dua->au_offset) > duhd->au_filesize) { |
| 1243 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1244 |
return DW_DLV_ERROR; |
| 1245 |
} |
| 1246 |
ASNAR(word_swap,dua->au_align,fa->align); |
| 1247 |
if (dua->au_align >= 32) { |
| 1248 |
*errcode = DW_DLE_UNIV_BIN_OFFSET_SIZE_ERROR; |
| 1249 |
return DW_DLV_ERROR; |
| 1250 |
} |
| 1251 |
ASNAR(word_swap,dua->au_reserved,fa->reserved); |
| 1252 |
} |
| 1253 |
return DW_DLV_OK; |
| 1254 |
} |
| 1255 |
|
| 1256 |
static const struct Dwarf_Universal_Head_s duhzero; |
| 1257 |
static const struct fat_header fhzero; |
| 1258 |
static int |
| 1259 |
_dwarf_object_detector_universal_head_fd( |
| 1260 |
int fd, |
| 1261 |
Dwarf_Unsigned dw_filesize, |
| 1262 |
unsigned int *dw_contentcount, |
| 1263 |
Dwarf_Universal_Head * dw_head, |
| 1264 |
int *errcode) |
| 1265 |
{ |
| 1266 |
struct Dwarf_Universal_Head_s duhd; |
| 1267 |
struct Dwarf_Universal_Head_s *duhdp = 0; |
| 1268 |
struct fat_header fh; |
| 1269 |
int res = 0; |
| 1270 |
void (*word_swap) (void *, const void *, unsigned long); |
| 1271 |
int locendian = 0; |
| 1272 |
int locoffsetsize = 0; |
| 1273 |
|
| 1274 |
duhd = duhzero; |
| 1275 |
fh = fhzero; |
| 1276 |
/* A universal head is always at offset zero. */ |
| 1277 |
duhd.au_filesize = dw_filesize; |
| 1278 |
if (sizeof(fh) >= dw_filesize) { |
| 1279 |
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR; |
| 1280 |
return DW_DLV_ERROR; |
| 1281 |
} |
| 1282 |
res = RRMOA(fd,&fh,0,sizeof(fh), dw_filesize,errcode); |
| 1283 |
if (res != DW_DLV_OK) { |
| 1284 |
return res; |
| 1285 |
} |
| 1286 |
duhd.au_magic = magic_copy((unsigned char *)&fh.magic[0],4); |
| 1287 |
if (duhd.au_magic == FAT_MAGIC) { |
| 1288 |
locendian = DW_END_big; |
| 1289 |
locoffsetsize = 32; |
| 1290 |
} else if (duhd.au_magic == FAT_CIGAM) { |
| 1291 |
locendian = DW_END_little; |
| 1292 |
locoffsetsize = 32; |
| 1293 |
}else if (duhd.au_magic == FAT_MAGIC_64) { |
| 1294 |
locendian = DW_END_big; |
| 1295 |
locoffsetsize = 64; |
| 1296 |
} else if (duhd.au_magic == FAT_CIGAM_64) { |
| 1297 |
locendian = DW_END_little; |
| 1298 |
locoffsetsize = 64; |
| 1299 |
} else { |
| 1300 |
*errcode = DW_DLE_FILE_WRONG_TYPE; |
| 1301 |
return DW_DLV_ERROR; |
| 1302 |
} |
| 1303 |
#ifdef WORDS_BIGENDIAN |
| 1304 |
if (locendian == DW_END_little) { |
| 1305 |
word_swap = _dwarf_memcpy_swap_bytes; |
| 1306 |
} else { |
| 1307 |
word_swap = _dwarf_memcpy_noswap_bytes; |
| 1308 |
} |
| 1309 |
#else /* LITTLE ENDIAN */ |
| 1310 |
if (locendian == DW_END_little) { |
| 1311 |
word_swap = _dwarf_memcpy_noswap_bytes; |
| 1312 |
} else { |
| 1313 |
word_swap = _dwarf_memcpy_swap_bytes; |
| 1314 |
} |
| 1315 |
#endif /* LITTLE- BIG-ENDIAN */ |
| 1316 |
ASNAR(word_swap,duhd.au_count,fh.nfat_arch); |
| 1317 |
/* The limit is a first-cut safe heuristic. */ |
| 1318 |
if (duhd.au_count >= (dw_filesize/2) ) { |
| 1319 |
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR ; |
| 1320 |
return DW_DLV_ERROR; |
| 1321 |
} |
| 1322 |
duhd.au_arches = (struct Dwarf_Universal_Arch_s*) |
| 1323 |
calloc(duhd.au_count, sizeof(struct Dwarf_Universal_Arch_s)); |
| 1324 |
if (!duhd.au_arches) { |
| 1325 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1326 |
return DW_DLV_ERROR; |
| 1327 |
} |
| 1328 |
if (locoffsetsize == 32) { |
| 1329 |
struct fat_arch * fa = 0; |
| 1330 |
|
| 1331 |
fa = (struct fat_arch *)calloc(duhd.au_count, |
| 1332 |
sizeof(struct fat_arch)); |
| 1333 |
if (!fa) { |
| 1334 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1335 |
free(duhd.au_arches); |
| 1336 |
duhd.au_arches = 0; |
| 1337 |
free(fa); |
| 1338 |
return DW_DLV_ERROR; |
| 1339 |
} |
| 1340 |
if (sizeof(fh)+duhd.au_count*sizeof(*fa) >= dw_filesize) { |
| 1341 |
free(duhd.au_arches); |
| 1342 |
duhd.au_arches = 0; |
| 1343 |
free(fa); |
| 1344 |
*errcode = DW_DLE_FILE_OFFSET_BAD; |
| 1345 |
return DW_DLV_ERROR; |
| 1346 |
} |
| 1347 |
res = RRMOA(fd,fa,/*offset=*/sizeof(fh), |
| 1348 |
duhd.au_count*sizeof(*fa), |
| 1349 |
dw_filesize,errcode); |
| 1350 |
if (res != DW_DLV_OK) { |
| 1351 |
free(duhd.au_arches); |
| 1352 |
duhd.au_arches = 0; |
| 1353 |
free(fa); |
| 1354 |
return res; |
| 1355 |
} |
| 1356 |
res = fill_in_uni_arch_32(fa,&duhd,word_swap, |
| 1357 |
errcode); |
| 1358 |
free(fa); |
| 1359 |
fa = 0; |
| 1360 |
if (res != DW_DLV_OK) { |
| 1361 |
free(duhd.au_arches); |
| 1362 |
duhd.au_arches = 0; |
| 1363 |
return res; |
| 1364 |
} |
| 1365 |
} else { /* 64 */ |
| 1366 |
struct fat_arch_64 * fa = 0; |
| 1367 |
fa = (struct fat_arch_64 *)calloc(duhd.au_count, |
| 1368 |
sizeof(struct fat_arch_64)); |
| 1369 |
if (!fa) { |
| 1370 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1371 |
free(duhd.au_arches); |
| 1372 |
duhd.au_arches = 0; |
| 1373 |
return DW_DLV_ERROR; |
| 1374 |
} |
| 1375 |
if (sizeof(fh)+duhd.au_count*sizeof(*fa) >= dw_filesize) { |
| 1376 |
free(duhd.au_arches); |
| 1377 |
duhd.au_arches = 0; |
| 1378 |
free(fa); |
| 1379 |
*errcode = DW_DLE_FILE_OFFSET_BAD ; |
| 1380 |
return DW_DLV_ERROR; |
| 1381 |
} |
| 1382 |
res = RRMOA(fd,fa,/*offset*/sizeof(fh), |
| 1383 |
duhd.au_count*sizeof(fa), |
| 1384 |
dw_filesize,errcode); |
| 1385 |
if (res == DW_DLV_ERROR) { |
| 1386 |
free(duhd.au_arches); |
| 1387 |
duhd.au_arches = 0; |
| 1388 |
free(fa); |
| 1389 |
return res; |
| 1390 |
} |
| 1391 |
res = fill_in_uni_arch_64(fa,&duhd,word_swap, |
| 1392 |
errcode); |
| 1393 |
free(fa); |
| 1394 |
fa = 0; |
| 1395 |
if (res != DW_DLV_OK) { |
| 1396 |
free(duhd.au_arches); |
| 1397 |
duhd.au_arches = 0; |
| 1398 |
return res; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
duhdp = malloc(sizeof(*duhdp)); |
| 1403 |
if (!duhdp) { |
| 1404 |
free(duhd.au_arches); |
| 1405 |
duhd.au_arches = 0; |
| 1406 |
*errcode = DW_DLE_ALLOC_FAIL; |
| 1407 |
return res; |
| 1408 |
} |
| 1409 |
memcpy(duhdp,&duhd,sizeof(duhd)); |
| 1410 |
*dw_contentcount = (unsigned int)duhd.au_count; |
| 1411 |
duhdp->au_arches = duhd.au_arches; |
| 1412 |
*dw_head = duhdp; |
| 1413 |
return DW_DLV_OK; |
| 1414 |
} |
| 1415 |
|
| 1416 |
int |
| 1417 |
_dwarf_object_detector_universal_instance( |
| 1418 |
Dwarf_Universal_Head dw_head, |
| 1419 |
Dwarf_Unsigned dw_index_of, |
| 1420 |
Dwarf_Unsigned *dw_cpu_type, |
| 1421 |
Dwarf_Unsigned *dw_cpusubtype, |
| 1422 |
Dwarf_Unsigned *dw_offset, |
| 1423 |
Dwarf_Unsigned *dw_size, |
| 1424 |
Dwarf_Unsigned *dw_align, |
| 1425 |
int *errcode) |
| 1426 |
{ |
| 1427 |
struct Dwarf_Universal_Arch_s* arch = 0; |
| 1428 |
|
| 1429 |
if (!dw_head) { |
| 1430 |
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR; |
| 1431 |
return DW_DLV_ERROR; |
| 1432 |
} |
| 1433 |
if (dw_index_of >= dw_head->au_count){ |
| 1434 |
return DW_DLV_NO_ENTRY; |
| 1435 |
} |
| 1436 |
arch = dw_head->au_arches +dw_index_of; |
| 1437 |
*dw_cpu_type = arch->au_cputype; |
| 1438 |
*dw_cpusubtype = arch->au_cpusubtype; |
| 1439 |
*dw_offset = arch->au_offset; |
| 1440 |
*dw_size = arch->au_size; |
| 1441 |
*dw_align = arch->au_align; |
| 1442 |
return DW_DLV_OK; |
| 1443 |
} |
| 1444 |
|
| 1445 |
void |
| 1446 |
_dwarf_dealloc_universal_head(Dwarf_Universal_Head dw_head) |
| 1447 |
{ |
| 1448 |
if (!dw_head) { |
| 1449 |
return; |
| 1450 |
} |
| 1451 |
free(dw_head->au_arches); |
| 1452 |
dw_head->au_arches = 0; |
| 1453 |
free(dw_head); |
| 1454 |
} |