Skip to content

Commit e612fb7

Browse files
committed
Now dwarfdump prints the number of binaries
in the Mach-O universal binary referenced and also prints the number (index) of the individual binary being reported on. modified: src/bin/dwarfdump/dd_command_options.c modified: src/bin/dwarfdump/dwarfdump.c modified: src/lib/libdwarf/dwarf_machoread.c modified: src/lib/libdwarf/dwarf_opaque.h modified: src/lib/libdwarf/dwarf_query.c new file: src/lib/libdwarf/dwarf_universal.h modified: src/lib/libdwarf/libdwarf.h
1 parent 3422281 commit e612fb7

File tree

7 files changed

+155
-39
lines changed

7 files changed

+155
-39
lines changed

src/bin/dwarfdump/dd_command_options.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ static const char *usage_long_text[] = {
598598
"-x groupnumber=<n> ",
599599
" --format-group-number=<n> Groupnumber to print",
600600
"-x universalnumber=<n> ",
601-
" --format-universalnumber=<n> Groupnumber to print",
602-
" non-zero only applies to",
601+
" --format-universalnumber=<n> Binary to print.",
602+
" Only applies to",
603603
" Mach-O universal binaries.",
604604
"-H<num> --format-limit=<num> Limit output to the first <num>",
605605
" major units.",

src/bin/dwarfdump/dwarfdump.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,21 @@ process_one_file(
949949
sanitized(temp_path_buf));
950950
glflags.gf_gnu_debuglink_flag = TRUE;
951951
} else { /* Nothing to print yet. */ }
952+
{
953+
Dwarf_Unsigned index = 0;
954+
Dwarf_Unsigned count = 0;
955+
dres = dwarf_get_universalbinary_count(dbg,&index,&count);
956+
if (dres == DW_DLV_OK) {
957+
const char * name = "object";
958+
if (count != 1) {
959+
name = "objects";
960+
}
961+
printf("This is a Mach-O Universal Binary with %"
962+
DW_PR_DUu " %s. This is object %"
963+
DW_PR_DUu "\n",
964+
count,name,index);
965+
}
966+
}
952967
if (tied_file_name && strlen(tied_file_name)) {
953968
{
954969
/* The tied file we define as group 1, BASE.

src/lib/libdwarf/dwarf_machoread.c

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9292
#include "dwarf_object_detector.h"
9393
#include "dwarf_macho_loader.h"
9494

95-
#if 1
95+
#if 0
9696
static void
9797
dump_bytes(const char *msg,Dwarf_Small * start, long len)
9898
{
@@ -142,6 +142,7 @@ static int _dwarf_macho_object_access_init(
142142
unsigned ftype,
143143
unsigned endian,
144144
unsigned offsetsize,
145+
unsigned * universalbinary_count,
145146
Dwarf_Unsigned filesize,
146147
Dwarf_Obj_Access_Interface_a **binary_interface,
147148
int *localerrnum);
@@ -327,12 +328,10 @@ load_macho_header32(dwarf_macho_object_access_internals_t *mfp,
327328
ASNAR(mfp->mo_copy_word,mfp->mo_header.flags,mh32.flags);
328329
mfp->mo_header.reserved = 0;
329330
mfp->mo_command_count = (unsigned int)mfp->mo_header.ncmds;
330-
printf("dadebug command count 0x%lx line %d\n",(unsigned long)mfp->mo_header.ncmds,__LINE__);
331331
if (mfp->mo_command_count >= mfp->mo_filesize ||
332332
mfp->mo_header.sizeofcmds >= mfp->mo_filesize ||
333333
mfp->mo_command_count >= mfp->mo_header.sizeofcmds) {
334334
*errcode = DW_DLE_MACHO_CORRUPT_HEADER;
335-
printf("dadebug DW_DLE_MACHO_CORRUPT_HEADER line %d\n",__LINE__);
336335
return DW_DLV_ERROR;
337336
}
338337

@@ -358,35 +357,21 @@ load_macho_header64(dwarf_macho_object_access_internals_t *mfp,
358357
if (res != DW_DLV_OK) {
359358
return res;
360359
}
361-
printf("dadebug header64 RRMOA 0x%lx\n",(unsigned long)inner);
362360
/* Do not adjust endianness of magic, leave as-is. */
363-
dump_bytes("dadebug mh64 ",(unsigned char *)&mh64,sizeof(mh64));
364361
ASNAR(memcpy,mfp->mo_header.magic,mh64.magic);
365362
ASNAR(mfp->mo_copy_word,mfp->mo_header.cputype,mh64.cputype);
366363
ASNAR(mfp->mo_copy_word,mfp->mo_header.cpusubtype,
367364
mh64.cpusubtype);
368365
ASNAR(mfp->mo_copy_word,mfp->mo_header.filetype,mh64.filetype);
369366
ASNAR(mfp->mo_copy_word,mfp->mo_header.ncmds,mh64.ncmds);
370-
printf("dadeug wordcpy addr 0x%lx\n",(unsigned long)mfp->mo_copy_word);
371-
printf("dadeug std wordcpy addr 0x%lx\n",(unsigned long)_dwarf_memcpy_swap_bytes);
372-
printf("dadebug command count 0x%lx line %d\n",(unsigned long)mfp->mo_header.ncmds,__LINE__);
373367
ASNAR(mfp->mo_copy_word,mfp->mo_header.sizeofcmds,
374368
mh64.sizeofcmds);
375-
printf("dadebug command count 0x%lx line %d\n",(unsigned long)mfp->mo_header.sizeofcmds,__LINE__);
376369
ASNAR(mfp->mo_copy_word,mfp->mo_header.flags,mh64.flags);
377370
ASNAR(mfp->mo_copy_word,mfp->mo_header.reserved,mh64.reserved);
378371
mfp->mo_command_count = (unsigned int)mfp->mo_header.ncmds;
379372
if (mfp->mo_command_count >= mfp->mo_filesize ||
380373
mfp->mo_header.sizeofcmds >= mfp->mo_filesize ||
381374
mfp->mo_command_count >= mfp->mo_header.sizeofcmds) {
382-
printf("dadebug "
383-
" comm count 0x%lx "
384-
" filesize 0x%lx "
385-
" sizeofcmds 0x%lx \n",
386-
(unsigned long)mfp->mo_command_count,
387-
(unsigned long)mfp->mo_filesize,
388-
(unsigned long)mfp->mo_header.sizeofcmds);
389-
printf("dadebug DW_DLE_MACHO_CORRUPT_HEADER line %d\n",__LINE__);
390375
*errcode = DW_DLE_MACHO_CORRUPT_HEADER;
391376
return DW_DLV_ERROR;
392377
}
@@ -890,11 +875,14 @@ _dwarf_macho_setup(int fd,
890875
dwarf_macho_object_access_internals_t *intfc = 0;
891876
int res = DW_DLV_OK;
892877
int localerrnum = 0;
878+
unsigned universalbinary_count = 0;
893879

894880
res = _dwarf_macho_object_access_init(
895881
fd,
896882
universalnumber,
897-
ftype,endian,offsetsize,filesize,
883+
ftype,endian,offsetsize,
884+
&universalbinary_count,
885+
filesize,
898886
&binary_interface,
899887
&localerrnum);
900888
if (res != DW_DLV_OK) {
@@ -914,6 +902,8 @@ _dwarf_macho_setup(int fd,
914902
}
915903
intfc = binary_interface->ai_object;
916904
intfc->mo_path = strdup(true_path);
905+
(*dbg)->de_universalbinary_index = universalnumber;
906+
(*dbg)->de_universalbinary_count = universalbinary_count;
917907
return res;
918908
}
919909

@@ -955,7 +945,6 @@ _dwarf_macho_inner_object_fd(int fd,
955945
Dwarf_Unsigned innerbase = 0;
956946
Dwarf_Unsigned innersize = 0;
957947

958-
printf("dadebug fd %d line %d\n",fd,__LINE__);
959948
res = _dwarf_object_detector_universal_head_fd(
960949
fd, outer_filesize, unibinarycount,
961950
&head, errcode);
@@ -1009,6 +998,7 @@ _dwarf_macho_object_access_internals_init(
1009998
unsigned ftype,
1010999
unsigned endian,
10111000
unsigned offsetsize,
1001+
unsigned *unibinarycount,
10121002
Dwarf_Unsigned filesize,
10131003
int *errcode)
10141004
{
@@ -1045,6 +1035,7 @@ _dwarf_macho_object_access_internals_init(
10451035
}
10461036
return res;
10471037
}
1038+
*unibinarycount = unibinarycounti;
10481039
endian = endiani;
10491040
}
10501041

@@ -1055,7 +1046,6 @@ _dwarf_macho_object_access_internals_init(
10551046
intfc->mo_fd = fd;
10561047
intfc->mo_offsetsize = offsetsizei;
10571048
intfc->mo_pointersize = offsetsizei;
1058-
printf("dadebug inner offset now 0x%lx\n",(unsigned long)fileoffseti);
10591049
intfc->mo_inner_offset = fileoffseti;
10601050
intfc->mo_filesize = filesizei;
10611051
intfc->mo_ftype = ftypei;
@@ -1074,11 +1064,9 @@ printf("dadebug inner offset now 0x%lx\n",(unsigned long)fileoffseti);
10741064
if (endian == DW_END_little ) {
10751065
intfc->mo_copy_word = _dwarf_memcpy_noswap_bytes;
10761066
intfc->mo_endian = DW_END_little;
1077-
printf("dadebug endian %lu line %d\n",(unsigned long)endian,__LINE__);
10781067
} else {
10791068
intfc->mo_copy_word = _dwarf_memcpy_swap_bytes;
10801069
intfc->mo_endian = DW_END_big;
1081-
printf("dadebug endian %lu line %d\n",(unsigned long)endian,__LINE__);
10821070
}
10831071
#endif /* LITTLE- BIG-ENDIAN */
10841072
res = _dwarf_load_macho_header(intfc,errcode);
@@ -1103,7 +1091,6 @@ printf("dadebug endian %lu line %d\n",(unsigned long)endian,__LINE__);
11031091
count better be zero. */
11041092
if (intfc->mo_dwarf_sectioncount) {
11051093
_dwarf_destruct_macho_access(localdoas);
1106-
printf("dadebug DW_DLE_MACHO_CORRUPT_HEADER line %d\n",__LINE__);
11071094
*errcode = DW_DLE_MACHO_CORRUPT_HEADER;
11081095
return DW_DLV_ERROR;
11091096
}
@@ -1130,6 +1117,7 @@ _dwarf_macho_object_access_init(
11301117
unsigned ftype,
11311118
unsigned endian,
11321119
unsigned offsetsize,
1120+
unsigned * universalbinary_count,
11331121
Dwarf_Unsigned filesize,
11341122
Dwarf_Obj_Access_Interface_a **binary_interface,
11351123
int *localerrnum)
@@ -1150,7 +1138,9 @@ _dwarf_macho_object_access_init(
11501138
res = _dwarf_macho_object_access_internals_init(internals,
11511139
fd,
11521140
uninumber,
1153-
ftype, endian, offsetsize, filesize,
1141+
ftype, endian, offsetsize,
1142+
universalbinary_count,
1143+
filesize,
11541144
localerrnum);
11551145
if (res != DW_DLV_OK){
11561146
/* *err is already set and the call freed internals. */
@@ -1290,16 +1280,12 @@ printf("Reading magic number universal compare failed " "Inconsistent\n");
12901280
ASNAR(word_swap,duhd.au_count,fh.nfat_arch);
12911281
/* The limit is a first-cut safe heuristic. */
12921282
if (duhd.au_count >= (dw_filesize/2) ) {
1293-
printf("dadebug Universal Binary header count impossible: 0x%lx \n",
1294-
(unsigned long) duhd.au_count);
12951283
*errcode = DW_DLE_UNIVERSAL_BINARY_ERROR ;
12961284
return DW_DLV_ERROR;
12971285
}
12981286
duhd.au_arches = (struct Dwarf_Universal_Arch_s*)
12991287
calloc(duhd.au_count, sizeof(struct Dwarf_Universal_Arch_s));
13001288
if (!duhd.au_arches) {
1301-
printf("dadebug Universal Binary au_arches alloc fail line %d\n",
1302-
__LINE__);
13031289
*errcode = DW_DLE_ALLOC_FAIL;
13041290
return DW_DLV_ERROR;
13051291
}
@@ -1308,8 +1294,6 @@ printf("dadebug Universal Binary au_arches alloc fail line %d\n",
13081294
fa = (struct fat_arch *)calloc(duhd.au_count,
13091295
sizeof(struct fat_arch));
13101296
if (!fa) {
1311-
printf("dadebug Universal Binary au_arches alloc fail line %d\n",
1312-
__LINE__);
13131297
*errcode = DW_DLE_ALLOC_FAIL;
13141298
free(duhd.au_arches);
13151299
duhd.au_arches = 0;
@@ -1320,8 +1304,6 @@ printf("dadebug Universal Binary au_arches alloc fail line %d\n",
13201304
duhd.au_count*sizeof(*fa),
13211305
dw_filesize,errcode);
13221306
if (res != DW_DLV_OK) {
1323-
printf("dadebug Universal Binary value read fail line %d\n",
1324-
__LINE__);
13251307
free(duhd.au_arches);
13261308
duhd.au_arches = 0;
13271309
free(fa);
@@ -1330,8 +1312,6 @@ __LINE__);
13301312
res = fill_in_uni_arch_32(fa,&duhd,word_swap);
13311313
if (res != DW_DLV_OK) {
13321314
free(duhd.au_arches);
1333-
printf("dadebug Universal Binary value read fail line %d\n",
1334-
__LINE__);
13351315
duhd.au_arches = 0;
13361316
free(fa);
13371317
return res;
@@ -1343,8 +1323,6 @@ __LINE__);
13431323
fa = (struct fat_arch_64 *)calloc(duhd.au_count,
13441324
sizeof(struct fat_arch));
13451325
if (!fa) {
1346-
printf("dadebug Universal Binary au_arches alloc fail line %d\n",
1347-
__LINE__);
13481326
*errcode = DW_DLE_ALLOC_FAIL;
13491327
free(duhd.au_arches);
13501328
duhd.au_arches = 0;
@@ -1354,7 +1332,6 @@ __LINE__);
13541332
duhd.au_count*sizeof(fa),
13551333
dw_filesize,errcode);
13561334
if (res == DW_DLV_ERROR) {
1357-
printf("dadebug Universal Binary value read fail line %d\n", __LINE__);
13581335
free(duhd.au_arches);
13591336
duhd.au_arches = 0;
13601337
free(fa);

src/lib/libdwarf/dwarf_opaque.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,14 @@ struct Dwarf_Debug_s {
754754
Dwarf_Unsigned de_frame_same_value_number;
755755
Dwarf_Unsigned de_frame_undefined_value_number;
756756

757+
/* If count > 0 means the DW_FTYPE_APPLEUNIVERSAL
758+
we initially read has this number of
759+
binaries in it, and de_universalbinary_index
760+
is the index of the current object inside
761+
the universal binary. */
762+
unsigned int de_universalbinary_count;
763+
unsigned int de_universalbinary_index;
764+
757765
unsigned char de_big_endian_object; /* Non-zero if
758766
object being read is big-endian. */
759767

src/lib/libdwarf/dwarf_query.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,3 +2106,25 @@ int dwarf_cu_header_basics(Dwarf_Die die,
21062106
}
21072107
return DW_DLV_OK;
21082108
}
2109+
2110+
int
2111+
dwarf_get_universalbinary_count(
2112+
Dwarf_Debug dbg,
2113+
Dwarf_Unsigned *current_index,
2114+
Dwarf_Unsigned *available_count)
2115+
{
2116+
if (!dbg) {
2117+
return DW_DLV_NO_ENTRY;
2118+
}
2119+
if (!dbg->de_universalbinary_count ) {
2120+
return DW_DLV_NO_ENTRY;
2121+
}
2122+
if (current_index) {
2123+
*current_index = dbg->de_universalbinary_index;
2124+
}
2125+
if (available_count) {
2126+
*available_count = dbg->de_universalbinary_count;
2127+
}
2128+
return DW_DLV_OK;
2129+
}
2130+

src/lib/libdwarf/dwarf_universal.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Copyright (c) 2023, David Anderson
2+
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+
#ifndef DWARF_UNIVERSAL_H
32+
#define DWARF_UNIVERSAL_H
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif /* __cplusplus */
37+
38+
struct Dwarf_Universal_Head_s;
39+
typedef struct Dwarf_Universal_Head_s * Dwarf_Universal_Head;
40+
41+
int _dwarf_object_detector_universal_head(
42+
char *dw_path,
43+
Dwarf_Unsigned dw_filesize,
44+
unsigned int *dw_contentcount,
45+
Dwarf_Universal_Head * dw_head,
46+
int *errcode);
47+
48+
int _dwarf_object_detector_universal_instance(
49+
Dwarf_Universal_Head dw_head,
50+
Dwarf_Unsigned dw_index_of,
51+
Dwarf_Unsigned *dw_cpu_type,
52+
Dwarf_Unsigned *dw_cpu_subtype,
53+
Dwarf_Unsigned *dw_offset,
54+
Dwarf_Unsigned *dw_size,
55+
Dwarf_Unsigned *dw_align,
56+
int *errcode);
57+
void _dwarf_dealloc_universal_head(Dwarf_Universal_Head dw_head);
58+
59+
#ifdef __cplusplus
60+
}
61+
#endif /* __cplusplus */
62+
63+
#endif /* DWARF_UNIVERSAL_H */

src/lib/libdwarf/libdwarf.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8872,6 +8872,37 @@ DW_API Dwarf_Small dwarf_set_default_address_size(
88728872
Dwarf_Debug dw_dbg,
88738873
Dwarf_Small dw_value);
88748874

8875+
/*! @brief Retrieve universal binary index
8876+
8877+
For Mach-O universal binaries this returns
8878+
relvant information.
8879+
8880+
For non-universal binaries (Mach-O, Elf,
8881+
or PE) the values are not meaningful, so
8882+
the function returns DW_DLV_NO_ENTRY..
8883+
8884+
@param dw_dbg
8885+
The Dwarf_Debug of interest.
8886+
@param dw_current_index
8887+
If dw_current_index is passed in non-null the function
8888+
returns the universal-binary index of the current
8889+
object (which came from a universal binary).
8890+
@param dw_available_count
8891+
If dw_current_index is passed in non-null the function
8892+
returns the count of binaries in
8893+
the universal binary.
8894+
@return
8895+
Returns DW_DLV_NO_ENTRY if the object file is
8896+
not from a Mach-O universal binary.
8897+
Returns DW_DLV_NO_ENTRY if dw_dbg is passed in NULL.
8898+
Never returns DW_DLV_ERROR.
8899+
*/
8900+
DW_API int dwarf_get_universalbinary_count(
8901+
Dwarf_Debug dw_dbg,
8902+
Dwarf_Unsigned *dw_current_index,
8903+
Dwarf_Unsigned *dw_available_count);
8904+
8905+
88758906
/*! @}
88768907
*/
88778908

0 commit comments

Comments
 (0)