如何更改ELF中的代码段的对齐方式 [英] How to change alignment of code segment in ELF

查看:879
本文介绍了如何更改ELF中的代码段的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ELF二进制中,如何更改可加载段的对齐方式?
在下面的例子中(见右下角),我想将0x200000减少到0x40960。

In ELF binary, how to change the alignment of loadable segments? In the below example (See right corner), I want to reduce the 0x200000 to 0x40960.

  LOAD  0x000000 0x000000400000 0x0000000000400000 0x000704 0x000704 R E **0x200000**
  LOAD  0x000e10 0x000000600e10 0x0000000000600e10 0x000230 0x000238 RW  **0x200000**


$ b b

任何编译器专家(GCC或clang)都能为我提供解决方案吗?

Can any compiler expert (GCC or clang), provide me a solution for this?

推荐答案

知道我真的想这样做,但您可以更改最大页面大小与 ld -z max-page-size = 4096

I don't know I you really want to do that but you can change the max page size with ld -z max-page-size=4096:

$ gcc foo.c && readelf -Wl ./a.out | grep LOAD 
LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x0008c4 0x0008c4 R E 0x200000
LOAD 0x0008c8 0x00000000006008c8 0x00000000006008c8 0x000250 0x000260 RW  0x200000
$ gcc foo.c -Wl,-z,max-page-size=4096 && readelf -Wl ./a.out | grep LOAD 
LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x0008c4 0x0008c4 R E 0x1000
LOAD 0x0008c8 0x00000000004018c8 0x00000000004018c8 0x000250 0x000260 RW  0x1000

很显然,原因是链接器尝试将段对齐到架构上可用的最大页面大小(在CPU上?)。标准默认页面大小为x86时为4KiB,但存在较大的页面(例如2MiB页面)。

Apparently, the reason is that the linker tries to align the segments to the maximum page size available on you architecture (on you CPU?). The standard default page size is 4KiB on x86 but greater pages exist (such as 2MiB pages).

这篇关于如何更改ELF中的代码段的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆