mach-o 工具

工具

command-line tools

  • lipo /usr/bin/lipo

能够分析二进制文件的架构,可以拆分和合并二进制文件

  • otool /usr/bin/otool

列出Mach-O文件的sections和segments信息,具体使用可以参考otool --help

  • pagestuff /usr/bin/pagestuff

展示每一个组成反射(image)的每个逻辑页面的内容,其中包含了sections的名字和每个page里的符号。这个工具不能在有多个架构的包含映射的二进制文件中运行。

  • symbol table的展示工具 /usr/bin/nm

允许你查看对象文件符号表的内容

工具的使用

lipo

The lipo command creates or operates on ``universal'' (multi-architec-
ture) files. It only ever produces one output file, and never alters
the input file. The operations that lipo performs are: listing the
architecture types in a universal file; creating a single universal
file from one or more input files; thinning out a single universal file
to one specified architecture type; and extracting, replacing, and/or
removing architectures types from the input file to create a single new
universal output file.
lipo 命令 是用来创建或者是操作一般含有多个结构体的文件。能输出一个文件,但不改变原文件。该命令能查询结构体类型,合并多个结构体文件,或者从多个结构体文件中剥离出来单个结构体。

Only one option can be specified, with the exception of -arch,
-arch_blank, -output, and -segalign, which are used in combination with
other options. The input_file argument is required, and only the -cre-
ate option allows more than one input_file to be specified. The -out-
put flag must be used, except with the -info and -detailed_info flags.

假设我们的moch-0二进制文件名字叫 test .结构体类型是armv7 和 arm64
lipo -info test

Architectures in the fat file: ETCP are: armv7 arm64 

lipo -detailed_info test

Fat header in: test
fat_magic 0xcafebabe
nfat_arch 2
architecture armv7
    cputype CPU_TYPE_ARM
    cpusubtype CPU_SUBTYPE_ARM_V7
    offset 16384
    size 37933760
    align 2^14 (16384)
architecture arm64
    cputype CPU_TYPE_ARM64
    cpusubtype CPU_SUBTYPE_ARM64_ALL
    offset 37961728
    size 43561328
    align 2^14 (16384)

lipo -thin armv7 test -output test.armv7

会在当前目录生成一个 test.armv7 结构体类型是 armv7

lipo -create test.arm64 test.armv7 -output test.all

将test.arm64和 test.armv7结构体 合并在一起输出到 test.all 文件中。

lipo -remove armv7 test.all -output test.arm64-1

将test.all 结构体中的armv7 移除掉

lipo -extract arm64 -output test.arm64-2 test.all

将test.all 结构体中的arm64 结构体输出到 test.arm64-2

lipo -create -arch armv7 test.armv7 -arch arm64 test.arm64 -output test.all

这里是把test.armv7 和 test.arm64 合并到test.all 文件中,-arch 只是用来修饰文件的,并且给文件做限制,必须是not-fat 结构体

otool /usr/bin/otool

The otool command displays specified parts of object files or libraries. If the -m option is not used the file arguments may be of the form libx.a(foo.o), to request information about only that object file and not the entire library. (Typically this argument must be quoted, ``libx.a(foo.o)'', to get it past the shell.) Otool understands both Mach-O (Mach object) files and universal file formats.Otool can display the specified information in either its raw (numeric)form (without the -v flag), or in a symbolic form using macro names of costants, etc. (with the -v or -V flag).

这个工具展示文件或者库的一部分。这个命令明白mach-o 文件和结构体文件。

    -f print the fat headers
    -a print the archive header
    -h print the mach header
    -l print the load commands
    -L print shared libraries used
    -D print shared library id name
    -t print the text section (disassemble with -v)
    -p <routine name>  start dissassemble from routine name
    -s <segname> <sectname> print contents of section
    -d print the data section
    -o print the Objective-C segment
    -r print the relocation entries
    -S print the table of contents of a library
    -T print the table of contents of a dynamic shared library
    -M print the module table of a dynamic shared library
    -R print the reference table of a dynamic shared library
    -I print the indirect symbol table
    -H print the two-level hints table
    -G print the data in code table
    -v print verbosely (symbolically) when possible
    -V print disassembled operands symbolically
    -c print argument strings of a core file
    -X print no leading addresses or headers
    -m don't use archive(member) syntax
    -B force Thumb disassembly (ARM objects only)
    -q use llvm's disassembler (the default)
    -Q use otool(1)'s disassembler
    -mcpu=arg use `arg' as the cpu for disassembly
    -j print opcode bytes
    -P print the info plist section as strings
    -C print linker optimization hints

pagestuff

mach-o 分析工具

pagestuff displays information about the specified logical pages of a file conforming to the Mach-O executable format. For each specified page of code, symbols (function and static data structure names) are displayed

命令举例

pagestuff test.arm64 -arch arm64 -a 

symbol table的展示工具

Nm displays the name list (symbol table) of each object file in the argument list. If an argument is an archive, a listing for each object file in the archive will be produced. File can be of the form libx.a(x.o), in which case only symbols from that member of the object file are listed. (The parentheses have to be quoted to get by the shell.) If no file is given, the symbols in a.out are listed.
nm 命令能展示每个文件的信号表

Each  symbol  name  is preceded by its value (blanks if undefined).  Unless the -m option is specified, this value is followed by one of the following characters, representing the  symbol  type:  U (undefined), A (absolute), T (text section symbol), D (data section symbol), B(bss section symbol), C (common symbol), -  (for  debugger  symbol  table  entries;  see  -a below), S (symbol in a section other than those above), or I (indirect symbol).  If the symbol is local (non-external), the symbol's type is instead represented by  the  corresponding lowercase  letter.   A lower case u in a dynamic shared library indicates a undefined reference to a private external in another module in the same library.

If the symbol is a Objective C method, the symbol name is +-[Class_name(category_name) method:name:], where +' is for class methods,-' is for instance methods, and (cate-
gory_name) is present only when the method is in a category.
这个是category 的标示方法



       -t format
              For the -P output, write the numeric value in the specified format. The format  shall
              be dependent on the single character used as the format option-argument:

       d      The value shall be written in decimal (default).

       o      The value shall be written in octal.

       x      The value shall be written in hexadecimal.

       -L     Display  the symbols in the bitcode files in the (__LLVM,__bundle) section if present
              instead of the object's symbol table.  This is the default if the object has no  sym-
              bol table and there is an (__LLVM,__bundle) section.

参数不少

这些工具暂时具体怎么使用还不太懂。只是略作记录,防止忘记。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,176评论 5 469
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,190评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,232评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,953评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,879评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,177评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,626评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,295评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,436评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,365评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,414评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,096评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,685评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,771评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,987评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,438评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,032评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,269评论 0 10
  • 有一段时间我特别喜欢转笔,因为看了香港的一部TVB侦探剧,里面的男二(非常帅)每次审案或者调查的时候总是会不自觉地...
    夏灵运阅读 286评论 0 0
  • 疲惫劳累的你 肩背酸疼的你 努力追赶金钱的你 却每每让金钱与你擦身而过 写满疲惫酸疼的背影 受...
    梦影3阅读 587评论 3 9
  • 我个人认为自己语文最快的提高期就是在高三的尾巴上,当时我确定自己考不上大学,老师也找我谈话:“某某某,离高考还有一...
    船长辛巴达阅读 166评论 2 4
  • 今天报了一个1777的课程,关于内在小孩深度疗愈的。 一直以来都很害怕面对自己,面对内在小孩,但是心里面明白,太多...
    喃嘸阿弥陀佛阅读 262评论 0 0