<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mach-O &#8211; 编程技术记录</title>
	<atom:link href="https://blog.z6z8.cn/tag/mach-o/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.z6z8.cn</link>
	<description>世界你好!</description>
	<lastBuildDate>Fri, 08 Nov 2019 03:40:57 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>使用__attribute __DATA 将常量写入Mach-O文件</title>
		<link>https://blog.z6z8.cn/2019/11/08/%e4%bd%bf%e7%94%a8__attribute-__data-%e5%b0%86%e5%b8%b8%e9%87%8f%e5%86%99%e5%85%a5mach-o%e6%96%87%e4%bb%b6/</link>
					<comments>https://blog.z6z8.cn/2019/11/08/%e4%bd%bf%e7%94%a8__attribute-__data-%e5%b0%86%e5%b8%b8%e9%87%8f%e5%86%99%e5%85%a5mach-o%e6%96%87%e4%bb%b6/#respond</comments>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Fri, 08 Nov 2019 03:40:57 +0000</pubDate>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[代码片段]]></category>
		<category><![CDATA[__DATA]]></category>
		<category><![CDATA[Mach-O]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=553</guid>

					<description><![CDATA[1、attribute DATA 宏 #define ZL_PULGIN_DATA(sectname) __a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>1、<strong>attribute </strong>DATA 宏</p>
<pre><code class="language-c">#define ZL_PULGIN_DATA(sectname) __attribute((used, section("__DATA,"#sectname" ")))
#define ZL_PULGIN(key,value) \
    const char * k##key##_##value##_zl ZL_PULGIN_DATA(ZLABCEDFG) = ""#key":"#value"";

//ZLABCEDFG 可以自由定义，但不能超过8个字符</code></pre>
<p>2、在实现文件如<code>.m</code>中使用</p>
<pre><code class="language-objc">ZL_PULGIN(hello,world)</code></pre>
<p>3、在运行时读取</p>
<p>读取函数</p>
<pre><code class="language-objc">#include &lt;mach-o/getsect.h&gt;
#include &lt;mach-o/loader.h&gt;
#include &lt;mach-o/dyld.h&gt;
#include &lt;dlfcn.h&gt;
#import &lt;objc/runtime.h&gt;
#import &lt;objc/message.h&gt;

static NSArray&lt;NSString *&gt;* BHReadConfiguration(const char *section)
{
    NSMutableArray *configs = [NSMutableArray array];

    Dl_info info;
    dladdr(BHReadConfiguration, &amp;info);

#ifndef __LP64__
    //        const struct mach_header *mhp = _dyld_get_image_header(0); // both works as below line
    const struct mach_header *mhp = (struct mach_header*)info.dli_fbase;
    unsigned long size = 0;
    uint32_t *memory = (uint32_t*)getsectiondata(mhp, "__DATA", section, &amp; size);
#else /* defined(__LP64__) */
    const struct mach_header_64 *mhp = (struct mach_header_64*)info.dli_fbase;
    unsigned long size = 0;
    uint64_t *memory = (uint64_t*)getsectiondata(mhp, "__DATA", section, &amp; size);
#endif /* defined(__LP64__) */

    for(int idx = 0; idx &lt; size/sizeof(void*); ++idx){
        char *string = (char*)memory[idx];

        NSString *str = [NSString stringWithUTF8String:string];
        if(!str)continue;

        if(str) [configs addObject:str];
    }

    return configs;

}</code></pre>
<p>执行结果</p>
<pre><code class="language-objc">NSArray *ret = BHReadConfiguration("ZLABCEDFG");

//ret 应为  ["hello:world"]</code></pre>
<p>其一个应用场景就是iOS的组件注册。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.z6z8.cn/2019/11/08/%e4%bd%bf%e7%94%a8__attribute-__data-%e5%b0%86%e5%b8%b8%e9%87%8f%e5%86%99%e5%85%a5mach-o%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
