Skip to content

Commit a12f601

Browse files
authored
Merge pull request rust-lang#1083 from bjorn3/fix_lld
Fix lld
2 parents e5b2b1b + 94f11cc commit a12f601

File tree

6 files changed

+91
-23
lines changed

6 files changed

+91
-23
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ function jit_calc() {
7070
object files when their content should have been changed by a change to cg_clif.</dd>
7171
<dt>CG_CLIF_DISPLAY_CG_TIME</dt>
7272
<dd>If "1", display the time it took to perform codegen for a crate</dd>
73+
<dt>CG_CLIF_FUNCTION_SECTIONS</dt>
74+
<dd>Use a single section for each function. This will often reduce the executable size at the
75+
cost of making linking significantly slower.</dd>
7376
</dl>
7477

7578
## Not yet supported

src/backend.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ impl WriteDebugInfo for ObjectProduct {
6868
.into_bytes();
6969

7070
let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
71-
let section_id = self.object.add_section(segment, name, SectionKind::Debug);
72-
self.object.section_mut(section_id).set_data(data, 1);
71+
// FIXME use SHT_X86_64_UNWIND for .eh_frame
72+
let section_id = self.object.add_section(segment, name.clone(), if id == SectionId::EhFrame {
73+
SectionKind::ReadOnlyData
74+
} else {
75+
SectionKind::Debug
76+
});
77+
self.object.section_mut(section_id).set_data(data, if id == SectionId::EhFrame {
78+
8
79+
} else {
80+
1
81+
});
7382
let symbol_id = self.object.section_symbol(section_id);
7483
(section_id, symbol_id)
7584
}
@@ -95,7 +104,7 @@ impl WriteDebugInfo for ObjectProduct {
95104
Relocation {
96105
offset: u64::from(reloc.offset),
97106
symbol,
98-
kind: RelocationKind::Absolute,
107+
kind: reloc.kind,
99108
encoding: RelocationEncoding::Generic,
100109
size: reloc.size * 8,
101110
addend: i64::try_from(symbol_offset).unwrap() + reloc.addend,
@@ -186,13 +195,17 @@ pub(crate) type Backend =
186195
impl cranelift_module::Backend<Product: AddConstructor + Emit + WriteDebugInfo>;
187196

188197
pub(crate) fn make_module(sess: &Session, name: String) -> Module<Backend> {
198+
let mut builder = ObjectBuilder::new(
199+
crate::build_isa(sess, true),
200+
name + ".o",
201+
cranelift_module::default_libcall_names(),
202+
)
203+
.unwrap();
204+
if std::env::var("CG_CLIF_FUNCTION_SECTIONS").is_ok() {
205+
builder.per_function_section(true);
206+
}
189207
let module: Module<ObjectBackend> = Module::new(
190-
ObjectBuilder::new(
191-
crate::build_isa(sess, true),
192-
name + ".o",
193-
cranelift_module::default_libcall_names(),
194-
)
195-
.unwrap(),
208+
builder,
196209
);
197210
module
198211
}

src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn data_id_for_alloc_id<B: Backend>(
274274
) -> DataId {
275275
module
276276
.declare_data(
277-
&format!("__alloc_{}", alloc_id.0),
277+
&format!("__alloc_{:x}", alloc_id.0),
278278
Linkage::Local,
279279
mutability == rustc_hir::Mutability::Mut,
280280
false,

src/debuginfo/emit.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(crate) struct DebugReloc {
4646
pub(crate) size: u8,
4747
pub(crate) name: DebugRelocName,
4848
pub(crate) addend: i64,
49+
pub(crate) kind: object::RelocationKind,
4950
}
5051

5152
#[derive(Clone)]
@@ -122,21 +123,21 @@ impl Writer for WriterRelocate {
122123
size,
123124
name: DebugRelocName::Symbol(symbol),
124125
addend: addend as i64,
126+
kind: object::RelocationKind::Absolute,
125127
});
126128
self.write_udata(0, size)
127129
}
128130
}
129131
}
130132

131-
// TODO: implement write_eh_pointer
132-
133133
fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
134134
let offset = self.len() as u32;
135135
self.relocs.push(DebugReloc {
136136
offset,
137137
size,
138138
name: DebugRelocName::Section(section),
139139
addend: val as i64,
140+
kind: object::RelocationKind::Absolute,
140141
});
141142
self.write_udata(0, size)
142143
}
@@ -153,7 +154,55 @@ impl Writer for WriterRelocate {
153154
size,
154155
name: DebugRelocName::Section(section),
155156
addend: val as i64,
157+
kind: object::RelocationKind::Absolute,
156158
});
157159
self.write_udata_at(offset, 0, size)
158160
}
161+
162+
fn write_eh_pointer(
163+
&mut self,
164+
address: Address,
165+
eh_pe: gimli::DwEhPe,
166+
size: u8,
167+
) -> Result<()> {
168+
match address {
169+
// Address::Constant arm copied from gimli
170+
Address::Constant(val) => {
171+
// Indirect doesn't matter here.
172+
let val = match eh_pe.application() {
173+
gimli::DW_EH_PE_absptr => val,
174+
gimli::DW_EH_PE_pcrel => {
175+
// TODO: better handling of sign
176+
let offset = self.len() as u64;
177+
offset.wrapping_sub(val)
178+
}
179+
_ => {
180+
return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
181+
}
182+
};
183+
self.write_eh_pointer_data(val, eh_pe.format(), size)
184+
}
185+
Address::Symbol { symbol, addend } => {
186+
match eh_pe.application() {
187+
gimli::DW_EH_PE_pcrel => {
188+
let size = match eh_pe.format() {
189+
gimli::DW_EH_PE_sdata4 => 4,
190+
_ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
191+
};
192+
self.relocs.push(DebugReloc {
193+
offset: self.len() as u32,
194+
size,
195+
name: DebugRelocName::Symbol(symbol),
196+
addend,
197+
kind: object::RelocationKind::Relative,
198+
});
199+
self.write_udata(0, size)
200+
}
201+
_ => {
202+
return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
203+
}
204+
}
205+
}
206+
}
207+
}
159208
}

src/debuginfo/unwind.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ impl<'tcx> UnwindContext<'tcx> {
1616
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
1717
let mut frame_table = FrameTable::default();
1818

19-
let cie_id = if let Some(cie) = isa.create_systemv_cie() {
19+
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
20+
if isa.flags().is_pic() {
21+
cie.fde_address_encoding = gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
22+
}
2023
Some(frame_table.add_cie(cie))
2124
} else {
2225
None

0 commit comments

Comments
 (0)