Skip to content

Commit e66b414

Browse files
authored
aot_resolve_target_info: Avoid in-place modification of e_type (#3564)
* I believe that LLVM MemoryBuffer interface is supposed to be read-only and it's allowed to use eg. read-only mmap of the underlying file. It isn't appropriate to modify the view at all. * in case of WASM_ENABLE_DEBUG_AOT, the whole buffer is written as the text section of the aot file. the modified e_type would confuse dwarf consumers. note that, even when we are using XIP, the debug info usually contains relocations. for example, llvm-dwarfdump doesn't seem to perform relocations on .debug_info section for ET_CORE (== 4 == our E_TYPE_XIP) objects.
1 parent f7d2826 commit e66b414

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,15 +3038,18 @@ typedef struct elf64_rela {
30383038
elf64_sxword r_addend;
30393039
} elf64_rela;
30403040

3041-
#define SET_TARGET_INFO(f, v, type, little) \
3042-
do { \
3043-
type tmp = elf_header->v; \
3044-
if ((little && !is_little_endian()) \
3045-
|| (!little && is_little_endian())) \
3046-
exchange_##type((uint8 *)&tmp); \
3047-
obj_data->target_info.f = tmp; \
3041+
#define SET_TARGET_INFO_VALUE(f, val, type, little) \
3042+
do { \
3043+
type tmp = val; \
3044+
if ((little && !is_little_endian()) \
3045+
|| (!little && is_little_endian())) \
3046+
exchange_##type((uint8 *)&tmp); \
3047+
obj_data->target_info.f = tmp; \
30483048
} while (0)
30493049

3050+
#define SET_TARGET_INFO_FIELD(f, v, type, little) \
3051+
SET_TARGET_INFO_VALUE(f, elf_header->v, type, little)
3052+
30503053
static bool
30513054
aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
30523055
{
@@ -3096,43 +3099,47 @@ aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
30963099
|| bin_type == LLVMBinaryTypeELF32B) {
30973100
struct elf32_ehdr *elf_header;
30983101
bool is_little_bin = bin_type == LLVMBinaryTypeELF32L;
3102+
uint16 e_type;
30993103

31003104
if (!elf_buf || elf_size < sizeof(struct elf32_ehdr)) {
31013105
aot_set_last_error("invalid elf32 buffer.");
31023106
return false;
31033107
}
31043108

31053109
elf_header = (struct elf32_ehdr *)elf_buf;
3110+
e_type = elf_header->e_type;
31063111

31073112
/* Emit eXecute In Place file type while in indirect mode */
31083113
if (comp_ctx->is_indirect_mode)
3109-
elf_header->e_type = E_TYPE_XIP;
3114+
e_type = E_TYPE_XIP;
31103115

3111-
SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
3112-
SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
3113-
SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
3114-
SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
3116+
SET_TARGET_INFO_VALUE(e_type, e_type, uint16, is_little_bin);
3117+
SET_TARGET_INFO_FIELD(e_machine, e_machine, uint16, is_little_bin);
3118+
SET_TARGET_INFO_FIELD(e_version, e_version, uint32, is_little_bin);
3119+
SET_TARGET_INFO_FIELD(e_flags, e_flags, uint32, is_little_bin);
31153120
}
31163121
else if (bin_type == LLVMBinaryTypeELF64L
31173122
|| bin_type == LLVMBinaryTypeELF64B) {
31183123
struct elf64_ehdr *elf_header;
31193124
bool is_little_bin = bin_type == LLVMBinaryTypeELF64L;
3125+
uint16 e_type;
31203126

31213127
if (!elf_buf || elf_size < sizeof(struct elf64_ehdr)) {
31223128
aot_set_last_error("invalid elf64 buffer.");
31233129
return false;
31243130
}
31253131

31263132
elf_header = (struct elf64_ehdr *)elf_buf;
3133+
e_type = elf_header->e_type;
31273134

31283135
/* Emit eXecute In Place file type while in indirect mode */
31293136
if (comp_ctx->is_indirect_mode)
3130-
elf_header->e_type = E_TYPE_XIP;
3137+
e_type = E_TYPE_XIP;
31313138

3132-
SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
3133-
SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
3134-
SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
3135-
SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
3139+
SET_TARGET_INFO_VALUE(e_type, e_type, uint16, is_little_bin);
3140+
SET_TARGET_INFO_FIELD(e_machine, e_machine, uint16, is_little_bin);
3141+
SET_TARGET_INFO_FIELD(e_version, e_version, uint32, is_little_bin);
3142+
SET_TARGET_INFO_FIELD(e_flags, e_flags, uint32, is_little_bin);
31363143
}
31373144
else if (bin_type == LLVMBinaryTypeMachO32L
31383145
|| bin_type == LLVMBinaryTypeMachO32B) {

0 commit comments

Comments
 (0)