翻译资料

This talk is going to be divided in two sections, the first is more theory and the second more practical, I’ll be doing the first theory part. And in it I’ll be walking you through all the steps that happen, all the way up to main.

But in order for you to understand and appreciate(领会) all the steps I first need to give you a crash course on Mach-O and Virtual Memory.

Mach-O is a bunch of file types for difference run time executables.

Executable——可执行 Main binary for application
dylib——a dynamic library, 动态库 (aka DSOs or DLLs)
bunlde——a special kind of dylan that you cannot link against, all you can do is load it at runtime by an dlopen and that’s used on a Mac OS for plug-ins.

image——refers to any of these three types. And I’ll be using that term a lot.

Framework——a dylib with a special directory structure around it to holds files needed by that dylan. So let’s dive right into the Mach-O image format.

Mach-O Image File:
1、segment
File divided into segments, by convention all segment names are use upper case letters. 文件分为几个部分,各部分命名用大写
Each segment is always a multiple of the page size, in this example the text is 3 pages, the DATA and LINKEDIT are each one page.
TEXT部分大小为3页,DATA和LINKEDIT部分大小都为1页
page size:页大小
the page size is determined by the hardware 页大小由硬件决定
arm64——16K
everything else——4K
2、section
sections is something the compiler omits.
sections are really just a subrange of a segment, they don’t have any of the constrains of being page size, but the are non-overlaying.
Common segments:
__TEXT has header, code, and read-only constants
__DATA has all read-write content: globals, static variables, etc.
__LINKEDIT doesn’t contain your functions of global variables, a LINKEDIT contains information about your function of variables such as their name and address.
You may have also heard of universal files, what are they?
Well suppose you build an iOS app, for a 64 bit, and now you have this Mach-O file, so what happens the next code when you say you also want to build it for 32 bit devices?
When you rebuild, Xcode will build another separate Mach-O file, this one built for 32 bits, RB7.
And then those two files are merged into a third file, called the Mach-O universal file.
And that has a header at the start, and all the header has a list of all the architecture and what their offsets are in the file.
在文件开始有个header,所有头部有个所有架构列表以及在文件中的位移位置。
And that header is also one page in size.
Now you may be wondering, why are the segments multiple page sizes?
Why is the header a page size, and it’s wasting a lot of space.
Well the reason everything is page based has to do with our next topic with is virtual memory.
So what is virtual memory?
虚拟内存

Virtual Memory is a level of indirection.
虚拟内存是中间层。

Every process is a logic address space which gets mapped to some physical page of RAM.
每个进程是个逻辑地址空间,和RAM里的物理页面相对应
Now this mapping does not have to be one to one, you could have logical address that go to no physical RAM and you can have multiple logical address that go to the same physical RAM.
这个映射不一定是一对一的,逻辑地址可能不指向物理内存,也可能多个逻辑地址指向同一个物理内存
This offered lots of opportunities here.
So what can you do with VM?
Well first, if you have a logical address that does not map to any physical RAM, when you access that address in your process, a page fault happens.
假设有个逻辑地址不和任何物理内存对应,访问进程中的该地址时,产生页错误。
At that point the kernel stops that thread and tries to figure out what needs to happen.
那时 内核会停止该线程,试图找出发生了什么。
The next thing is if you have two processes, with different logical addresses, mapping to the same physical page, those two processes are now sharing the same bit of RAM.
另外一件事,如果你有两个不同逻辑地址的进程,对应同一块物理页,这两个进程共享同一块内存。
You now have sharing between processes.

Another interesting feature is file backed mapping.
另一个有趣的特点是备份文件的映射
Rather than actually read an entire file into RAM you can tell the VM system through the mmap call, then I want this slice of this file mapped to this address range in my process.
而不是实际读取整个文件到RAM中,您可以通过mmap调用告诉VM系统,我想要将这个文件的这个切片映射到我的进程中的这个地址范围。
So why would you do that? Well rather than having to read the entire file, by having that mapping set up, as you first access those difference addresses, as if you had read it in memory, each time you access an address that hasn’t been accessed before it will cause a page fault, the kernel will read just that one page.
为什么要这么做?不必读取整个文件,通过映射设置,当您第一次访问这些不同的地址,如果您读过它在内存中,

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

推荐阅读更多精彩内容