generate read-only code image in bootimage build

This avoids the requirement of putting the code image in a
section/segment which is both writable and executable, which is good
for security and avoids trouble with systems like iOS which disallow
such things.

The implementation relies on relative addressing such that the offset
of the desired address is fixed as a compile-time constant relative to
the start of the memory area of interest (e.g. the code image, heap
image, or thunk table).  At runtime, the base pointer to the memory
area is retrieved from the thread structure and added to the offset to
compute the final address.  Using the thread pointer allows us to
generate read-only, position-independent code while avoiding the use
of IP-relative addressing, which is not available on all
architectures.
This commit is contained in:
Joel Dice
2011-09-20 16:30:30 -06:00
parent 349d381d95
commit c537dcfd34
15 changed files with 478 additions and 416 deletions

View File

@ -381,12 +381,14 @@ MAKE_NAME(writeElf, BITS_PER_WORD, Object)
const char* sectionName;
unsigned sectionFlags = SHF_ALLOC;
if (writable and executable) {
sectionName = ".rwx";
sectionFlags |= SHF_WRITE | SHF_EXECINSTR;
} else if (writable) {
sectionName = ".data";
sectionFlags |= SHF_WRITE;
if (writable) {
if (executable) {
sectionName = ".rwx";
sectionFlags |= SHF_WRITE | SHF_EXECINSTR;
} else {
sectionName = ".data";
sectionFlags |= SHF_WRITE;
}
} else if (executable) {
sectionName = ".text";
sectionFlags |= SHF_EXECINSTR;

View File

@ -306,7 +306,7 @@ bool
MAKE_NAME(writeMachO, BITS_PER_WORD, Object)
(uint8_t* data, unsigned size, FILE* out, const char* startName,
const char* endName, const char* architecture, unsigned alignment,
bool writable, bool)
bool writable, bool executable)
{
cpu_type_t cpuType;
cpu_subtype_t cpuSubType;
@ -330,8 +330,13 @@ MAKE_NAME(writeMachO, BITS_PER_WORD, Object)
const char* segmentName;
const char* sectionName;
if (writable) {
segmentName = "__RWX";
sectionName = "__rwx";
if (executable) {
segmentName = "__RWX";
sectionName = "__rwx";
} else {
segmentName = "__DATA";
sectionName = "__data";
}
} else {
segmentName = "__TEXT";
sectionName = "__text";

View File

@ -207,17 +207,19 @@ writePEObject
sectionMask |= IMAGE_SCN_MEM_READ;
const char* sectionName;
if (writable and executable) {
sectionName = ".rwx";
sectionMask |= IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_CNT_CODE;
} else if (executable) {
if (writable) {
if (executable) {
sectionName = ".rwx";
sectionMask |= IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_CNT_CODE;
} else {
sectionName = ".data";
sectionMask |= IMAGE_SCN_MEM_WRITE;
}
} else {
sectionName = ".text";
sectionMask |= IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE;
} else {
sectionName = ".data";
sectionMask |= IMAGE_SCN_MEM_WRITE;
}
writeObject(data, size, out, startName, endName, sectionName, machine,