How can I get GCC to compile the .text section as being written to the ELF binary?

I would like to be able to dynamically change the executable code in the library that I use. Essentially, I would like to dynamically NOP perform certain functions if they are not needed.

However, the .text section of the library that I use is not writable (as is the case for most programs). I have the source code of the library, and I would like to use GCC to compile it as writable.

Is there any way to do this?

+7
source share
4 answers

In a general sense, mprotect is a perceptual choice (on POSIX-compatible systems) under sys/mman.h (check http://linux.die.net/man/2/mprotect ), just get the number of address and system pages of your executable section process and call mprotect to request permissions; write him; then call mprotect again to release write permission.

However, if this is intended for low-level routines where speed is absolute (or mprotect not available), then you will want to compile the library with its .text section, accessible as the calling mprotect most likely will cause a translation Lookaside Buffer (TLB), which ( especially in a multiprocessor environment) can and will cause a bottleneck. If a particular system uses hardware protection via paging (which is almost all now), then the only way to change the protection is to create a TLB flash that must be executed on each link page that links to the page table (page group) that is referenced (page table group ) and each processor. To do this, you need to execute in ring 0, which requires a system column that simply imposes a cherry on top of the overhead.

In the latter case, the simplest solution would be to compile the library normally and then objcopy using --writable-text (as indicated by ggiroux).

Another solution would be to define the linker.ld layout linker linker.ld yourself. Then you can explicitly specify permissions for any section. It is not too complicated; if it depends on the system. See the documentation at http://www.math.utah.edu/docs/info/ld_3.html . You can also view your linker.ld system linker.ld and modify it from there. Passing -Wl,--verbose to gcc will instruct the linker to pluck out all the relevant files (including its default linker), in which you could then change the permissions of the .text section and recompile the library (forever) using the new linker.ld file.

To summarize, my recommendation would be to make, as the last paragraph, and compile your library with a slightly modified linker script.

+7
source

Try objcopy --writable-text in a compiled library, according to the documentation, it should make .text writable.

+5
source

The easiest way I've found (binutils 2.22) is to associate with -N This can be passed to gcc with gcc -XN

+2
source

Probably the best approach is to use the system API to change the write capability in memory that you want to change, change, and then change.

On unix family systems, you want to see mprotect . Just keep in mind that it deals with blocks that are multiples of the page size of your system. Probably 4096, so rounding is likely to be required.

+1
source

All Articles