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.
Ryan leckey
source share