On different OS, the object file's (eg: generated a .c file into a .o file by gcc) formats are different, just like CS:APP, chapter 7.3 said:
Object file formats vary from system to system. The first Unix systems from Bell Labs used the a.out format. (To this day, executables are still referred to as a.out files.) Early versions of System V Unix used the Common Object File format (COFF). Windows NT uses a variant of COFF called the Portable Executable (PE) format. Modern Unix systems—such as Linux, later versions of System V Unix, BSD Unix variants, and Sun Solaris—use the Unix Executable and Linkable Format (ELF). Although our discussion will focus on ELF, the basic concepts are similar, regardless of the particular format.
When I generated my .c file into object file on my mac and then use greadelf(readelf's mac version from binutils), i got the following error msg:
Error: Not an ELF file - it has the wrong magic bytes at the start
So I use a command to ensure the format of a object file on mac:
$ file p
p: Mach-O 64-bit executable x86_64
As you can see, mac os use the Mach-O object file format, however Linux uses the ELF object file format. They perform the same general purpose but in completely different ways.
So we can's use readelf command with object files on mac os.
otool is OSX's objdump, we can use otool -l to read .o file's information
$ otool -l prog
reference: mach-binaries
网友评论