Skip to content

Commit 72cf4ca

Browse files
authored
Fix section/symbol flags when setting the used flag (#11231)
1 parent b3ec490 commit 72cf4ca

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

cranelift/object/src/backend.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use object::write::{
1616
};
1717
use object::{
1818
RelocationEncoding, RelocationFlags, RelocationKind, SectionFlags, SectionKind, SymbolFlags,
19-
SymbolKind, SymbolScope,
19+
SymbolKind, SymbolScope, elf,
2020
};
2121
use std::collections::HashMap;
2222
use std::collections::hash_map::Entry;
@@ -458,12 +458,20 @@ impl Module for ObjectModule {
458458
let section = self.object.section_mut(section);
459459
match &mut section.flags {
460460
SectionFlags::None => {
461+
// Explicitly specify default flags as SectionFlags::Elf overwrites them
462+
let sh_flags = if decl.tls {
463+
elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_TLS
464+
} else if decl.writable || !relocs.is_empty() {
465+
elf::SHF_ALLOC | elf::SHF_WRITE
466+
} else {
467+
elf::SHF_ALLOC
468+
};
461469
section.flags = SectionFlags::Elf {
462-
sh_flags: object::elf::SHF_GNU_RETAIN.into(),
470+
sh_flags: u64::from(sh_flags | elf::SHF_GNU_RETAIN),
463471
}
464472
}
465473
SectionFlags::Elf { sh_flags } => {
466-
*sh_flags |= u64::from(object::elf::SHF_GNU_RETAIN)
474+
*sh_flags |= u64::from(elf::SHF_GNU_RETAIN)
467475
}
468476
_ => unreachable!(),
469477
}
@@ -472,8 +480,13 @@ impl Module for ObjectModule {
472480
object::BinaryFormat::MachO => {
473481
let symbol = self.object.symbol_mut(symbol);
474482
assert!(matches!(symbol.flags, SymbolFlags::None));
483+
let n_desc = if decl.linkage == Linkage::Preemptible {
484+
object::macho::N_WEAK_DEF
485+
} else {
486+
0
487+
};
475488
symbol.flags = SymbolFlags::MachO {
476-
n_desc: object::macho::N_NO_DEAD_STRIP,
489+
n_desc: n_desc | object::macho::N_NO_DEAD_STRIP,
477490
}
478491
}
479492
_ => unreachable!(),

0 commit comments

Comments
 (0)