@@ -12,6 +12,7 @@ use crate::platform::RelocationSequence as _;
1212use crate :: platform:: SourceInfo ;
1313use crate :: platform:: SourceInfoDetails ;
1414use anyhow:: Context ;
15+ use linker_utils:: elf:: RelocationKind ;
1516use object:: LittleEndian ;
1617use object:: read:: elf:: Crel ;
1718use object:: read:: elf:: RelocationSections ;
@@ -144,30 +145,39 @@ fn apply_section_relocations<A: Arch<Platform = crate::elf::Elf>, R: Relocation>
144145 . get ( LittleEndian )
145146 . wrapping_add ( rel. addend ( ) as u64 ) ;
146147
147- let section_index = object
148- . symbol_section ( symbol, sym_index) ?
149- . context ( "Relocation for undefined symbol" ) ?;
148+ let Some ( section_index) = object. symbol_section ( symbol, sym_index) ? else {
149+ // Ignore undefined symbols.
150+ continue ;
151+ } ;
150152 let symbol_section = object. section ( section_index) ?;
151153
152154 let data_offset = rel. offset ( ) as usize ;
153155
154- if symbol_section. sh_offset . get ( LittleEndian )
155- == section_of_interest. sh_offset . get ( LittleEndian )
156- {
157- value += SECTION_LOAD_ADDRESS ;
158- }
159-
160156 let r_type = A :: relocation_from_raw ( rel. raw_type ( ) ) ?;
161157
162158 let linker_utils:: elf:: RelocationSize :: ByteSize ( num_bytes) = r_type. size else {
163159 continue ;
164160 } ;
165161
166- if r_type. kind == linker_utils:: elf:: RelocationKind :: Absolute {
167- section_data
168- . get_mut ( data_offset..data_offset + num_bytes)
169- . context ( "Invalid relocation offset" ) ?
170- . copy_from_slice ( & value. to_le_bytes ( ) [ ..num_bytes] ) ;
162+ let rel_out = section_data
163+ . get_mut ( data_offset..data_offset + num_bytes)
164+ . context ( "Invalid relocation offset" ) ?;
165+
166+ match r_type. kind {
167+ RelocationKind :: Absolute => {
168+ let in_section_of_interest = symbol_section. sh_offset . get ( LittleEndian )
169+ == section_of_interest. sh_offset . get ( LittleEndian ) ;
170+
171+ if in_section_of_interest {
172+ value += SECTION_LOAD_ADDRESS ;
173+ }
174+
175+ r_type. write_to_buffer ( value, rel_out) ?;
176+ }
177+ RelocationKind :: AbsoluteAddition | RelocationKind :: AbsoluteSubtraction => {
178+ r_type. write_to_buffer ( value, rel_out) ?;
179+ }
180+ _ => { }
171181 }
172182 }
173183 Ok ( ( ) )
0 commit comments