Skip to content

Commit

Permalink
write: fix Address variant names
Browse files Browse the repository at this point in the history
It is possible (and common) for a symbol to use an absolute relocation.
  • Loading branch information
philipc committed Apr 19, 2019
1 parent 70c3730 commit 998c21c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
24 changes: 12 additions & 12 deletions src/write/cfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,9 @@ pub(crate) mod convert {
///
/// `convert_address` is a function to convert read addresses into the `Address`
/// type. For non-relocatable addresses, this function may simply return
/// `Address::Absolute(address)`. For relocatable addresses, it is the caller's
/// `Address::Constant(address)`. For relocatable addresses, it is the caller's
/// responsibility to determine the symbol and addend corresponding to the address
/// and return `Address::Relative { symbol, addend }`.
/// and return `Address::Symbol { symbol, addend }`.
pub fn from<R: Reader<Offset = usize>>(
frame: &read::DebugFrame<R>,
convert_address: &dyn Fn(u64) -> Option<Address>,
Expand Down Expand Up @@ -784,24 +784,24 @@ mod tests {

let mut cie2 = CommonInformationEntry::new(encoding, 1, 8, X86_64::RA);
cie2.lsda = true;
cie2.personality = Some(Address::Absolute(0x1234));
cie2.personality = Some(Address::Constant(0x1234));
cie2.signal_trampoline = true;
let cie2_id = frames.add_cie(cie2.clone());
assert_ne!(cie1_id, cie2_id);
assert_eq!(cie2_id, frames.add_cie(cie2.clone()));

let fde1 = FrameDescriptionEntry::new(Address::Absolute(0x1000), 0x10);
let fde1 = FrameDescriptionEntry::new(Address::Constant(0x1000), 0x10);
frames.add_fde(cie1_id, fde1.clone());

let fde2 = FrameDescriptionEntry::new(Address::Absolute(0x2000), 0x20);
let fde2 = FrameDescriptionEntry::new(Address::Constant(0x2000), 0x20);
frames.add_fde(cie1_id, fde2.clone());

let mut fde3 = FrameDescriptionEntry::new(Address::Absolute(0x3000), 0x30);
fde3.lsda = Some(Address::Absolute(0x3300));
let mut fde3 = FrameDescriptionEntry::new(Address::Constant(0x3000), 0x30);
fde3.lsda = Some(Address::Constant(0x3300));
frames.add_fde(cie2_id, fde3.clone());

let mut fde4 = FrameDescriptionEntry::new(Address::Absolute(0x4000), 0x40);
fde4.lsda = Some(Address::Absolute(0x4400));
let mut fde4 = FrameDescriptionEntry::new(Address::Constant(0x4000), 0x40);
fde4.lsda = Some(Address::Constant(0x4400));
frames.add_fde(cie2_id, fde4.clone());

let mut debug_frame = DebugFrame::from(EndianVec::new(LittleEndian));
Expand All @@ -811,7 +811,7 @@ mod tests {
read::DebugFrame::new(debug_frame.slice(), LittleEndian);
read_debug_frame.set_address_size(address_size);
let convert_frames = FrameTable::from(&read_debug_frame, &|address| {
Some(Address::Absolute(address))
Some(Address::Constant(address))
})
.unwrap();
assert_eq!(frames.cies, convert_frames.cies);
Expand Down Expand Up @@ -880,7 +880,7 @@ mod tests {
}
let cie_id = frames.add_cie(cie);

let mut fde = FrameDescriptionEntry::new(Address::Absolute(0x1000), 0x10);
let mut fde = FrameDescriptionEntry::new(Address::Constant(0x1000), 0x10);
for (o, i) in &fde_instructions {
fde.add_instruction(*o, i.clone());
}
Expand All @@ -893,7 +893,7 @@ mod tests {
read::DebugFrame::new(debug_frame.slice(), LittleEndian);
read_debug_frame.set_address_size(address_size);
let frames = FrameTable::from(&read_debug_frame, &|address| {
Some(Address::Absolute(address))
Some(Address::Constant(address))
})
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions src/write/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ pub(crate) mod convert {
///
/// `convert_address` is a function to convert read addresses into the `Address`
/// type. For non-relocatable addresses, this function may simply return
/// `Address::Absolute(address)`. For relocatable addresses, it is the caller's
/// `Address::Constant(address)`. For relocatable addresses, it is the caller's
/// responsibility to determine the symbol and addend corresponding to the address
/// and return `Address::Relative { symbol, addend }`.
/// and return `Address::Symbol { symbol, addend }`.
pub fn from<R: Reader<Offset = usize>>(
dwarf: &read::Dwarf<R>,
convert_address: &dyn Fn(u64) -> Option<Address>,
Expand Down
10 changes: 5 additions & 5 deletions src/write/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ mod tests {

let read_debug_line = read::DebugLine::new(debug_line.slice(), LittleEndian);

let convert_address = &|address| Some(Address::Absolute(address));
let convert_address = &|address| Some(Address::Constant(address));
for ((program, file_id, encoding), offset) in programs.iter().zip(debug_line_offsets.iter())
{
let read_program = read_debug_line
Expand Down Expand Up @@ -1272,7 +1272,7 @@ mod tests {
let dir1 = &b"dir1"[..];
let file1 = &b"file1"[..];
let file2 = &b"file2"[..];
let convert_address = &|address| Some(Address::Absolute(address));
let convert_address = &|address| Some(Address::Constant(address));

let debug_line_str_offsets = DebugLineStrOffsets::none();
let debug_str_offsets = DebugStrOffsets::none();
Expand Down Expand Up @@ -1307,7 +1307,7 @@ mod tests {
// Test sequences.
{
let mut program = program.clone();
let address = Address::Absolute(0x12);
let address = Address::Constant(0x12);
program.begin_sequence(Some(address));
assert_eq!(
program.instructions,
Expand Down Expand Up @@ -1657,7 +1657,7 @@ mod tests {
read::LineInstruction::EndSequence,
),
(
LineInstruction::SetAddress(Address::Absolute(0x12)),
LineInstruction::SetAddress(Address::Constant(0x12)),
read::LineInstruction::SetAddress(0x12),
),
(
Expand Down Expand Up @@ -1740,7 +1740,7 @@ mod tests {
None,
);
for address_advance in addresses.clone() {
program.begin_sequence(Some(Address::Absolute(0x1000)));
program.begin_sequence(Some(Address::Constant(0x1000)));
program.row().line = 0x10000;
program.generate_row();
for line_advance in lines.clone() {
Expand Down
8 changes: 4 additions & 4 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! let mut dwarf = DwarfUnit::new(encoding);
//! // Set a range attribute on the root DIE.
//! let range_list = RangeList(vec![Range::StartLength {
//! begin: Address::Absolute(0x100),
//! begin: Address::Constant(0x100),
//! length: 42,
//! }]);
//! let range_list_id = dwarf.unit.ranges.add(range_list);
Expand Down Expand Up @@ -237,10 +237,10 @@ pub type Result<T> = result::Result<T, Error>;
/// An address.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address {
/// An absolute address that does not require relocation.
Absolute(u64),
/// A fixed address that does not require relocation.
Constant(u64),
/// An address that is relative to a symbol which may be relocated.
Relative {
Symbol {
/// The symbol that the address is relative to.
///
/// The meaning of this value is decided by the writer, but
Expand Down
24 changes: 12 additions & 12 deletions src/write/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl RangeListTable {
}
Range::StartLength { begin, length } => {
let end = match begin {
Address::Absolute(begin) => Address::Absolute(begin + length),
Address::Relative { symbol, addend } => Address::Relative {
Address::Constant(begin) => Address::Constant(begin + length),
Address::Symbol { symbol, addend } => Address::Symbol {
symbol,
addend: addend + length as i64,
},
Expand Down Expand Up @@ -223,7 +223,7 @@ mod convert {
mut from: read::RawRngListIter<R>,
context: &ConvertUnitContext<R>,
) -> ConvertResult<Self> {
let mut have_base_address = context.base_address != Address::Absolute(0);
let mut have_base_address = context.base_address != Address::Constant(0);
let convert_address =
|x| (context.convert_address)(x).ok_or(ConvertError::InvalidAddress);
let mut ranges = Vec::new();
Expand All @@ -234,7 +234,7 @@ mod convert {
let begin = convert_address(begin)?;
let end = convert_address(end)?;
match (begin, end) {
(Address::Absolute(begin_offset), Address::Absolute(end_offset)) => {
(Address::Constant(begin_offset), Address::Constant(end_offset)) => {
if have_base_address {
Range::OffsetPair {
begin: begin_offset,
Expand Down Expand Up @@ -322,15 +322,15 @@ mod tests {

let mut range_list = RangeList(vec![
Range::StartLength {
begin: Address::Absolute(6666),
begin: Address::Constant(6666),
length: 7777,
},
Range::StartEnd {
begin: Address::Absolute(4444),
end: Address::Absolute(5555),
begin: Address::Constant(4444),
end: Address::Constant(5555),
},
Range::BaseAddress {
address: Address::Absolute(1111),
address: Address::Constant(1111),
},
Range::OffsetPair {
begin: 2222,
Expand Down Expand Up @@ -380,17 +380,17 @@ mod tests {
line_strings: &mut line_strings,
strings: &mut strings,
ranges: &mut ranges,
convert_address: &|address| Some(Address::Absolute(address)),
base_address: Address::Absolute(0),
convert_address: &|address| Some(Address::Constant(address)),
base_address: Address::Constant(0),
line_program_offset: None,
line_program_files: Vec::new(),
};
let convert_range_list = RangeList::from(read_range_list, &context).unwrap();

if version <= 4 {
range_list.0[0] = Range::StartEnd {
begin: Address::Absolute(6666),
end: Address::Absolute(6666 + 7777),
begin: Address::Constant(6666),
end: Address::Constant(6666 + 7777),
};
}
assert_eq!(range_list, convert_range_list);
Expand Down
24 changes: 12 additions & 12 deletions src/write/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,9 +1184,9 @@ pub(crate) mod convert {
///
/// `convert_address` is a function to convert read addresses into the `Address`
/// type. For non-relocatable addresses, this function may simply return
/// `Address::Absolute(address)`. For relocatable addresses, it is the caller's
/// `Address::Constant(address)`. For relocatable addresses, it is the caller's
/// responsibility to determine the symbol and addend corresponding to the address
/// and return `Address::Relative { symbol, addend }`.
/// and return `Address::Symbol { symbol, addend }`.
pub fn from<R: Reader<Offset = usize>>(
dwarf: &read::Dwarf<R>,
line_strings: &mut write::LineStringTable,
Expand Down Expand Up @@ -1819,7 +1819,7 @@ mod tests {
&dwarf,
&mut convert_line_strings,
&mut convert_strings,
&|address| Some(Address::Absolute(address)),
&|address| Some(Address::Constant(address)),
)
.unwrap();
assert_eq!(convert_units.count(), units.count());
Expand Down Expand Up @@ -1851,8 +1851,8 @@ mod tests {
let string_id = strings.add("string two");
let mut ranges = RangeListTable::default();
let range_id = ranges.add(RangeList(vec![Range::StartEnd {
begin: Address::Absolute(0x1234),
end: Address::Absolute(0x2345),
begin: Address::Constant(0x1234),
end: Address::Constant(0x2345),
}]));

let mut debug_str = DebugStr::from(EndianVec::new(LittleEndian));
Expand Down Expand Up @@ -1904,7 +1904,7 @@ mod tests {
for &(ref name, ref value, ref expect_value) in &[
(
constants::DW_AT_name,
AttributeValue::Address(Address::Absolute(0x1234)),
AttributeValue::Address(Address::Constant(0x1234)),
read::AttributeValue::Addr(0x1234),
),
(
Expand Down Expand Up @@ -2132,8 +2132,8 @@ mod tests {
line_strings: &mut line_strings,
strings: &mut strings,
ranges: &mut ranges,
convert_address: &|address| Some(Address::Absolute(address)),
base_address: Address::Absolute(0),
convert_address: &|address| Some(Address::Constant(address)),
base_address: Address::Constant(0),
line_program_offset: None,
line_program_files: Vec::new(),
};
Expand Down Expand Up @@ -2300,7 +2300,7 @@ mod tests {
&dwarf,
&mut convert_line_strings,
&mut convert_strings,
&|address| Some(Address::Absolute(address)),
&|address| Some(Address::Constant(address)),
)
.unwrap();
assert_eq!(convert_units.count(), units.count());
Expand Down Expand Up @@ -2506,7 +2506,7 @@ mod tests {
&dwarf,
&mut convert_line_strings,
&mut convert_strings,
&|address| Some(Address::Absolute(address)),
&|address| Some(Address::Constant(address)),
)
.unwrap();

Expand Down Expand Up @@ -2602,8 +2602,8 @@ mod tests {
line_strings: &mut line_strings,
strings: &mut strings,
ranges: &mut ranges,
convert_address: &|address| Some(Address::Absolute(address)),
base_address: Address::Absolute(0),
convert_address: &|address| Some(Address::Constant(address)),
base_address: Address::Constant(0),
line_program_offset: Some(line_program_offset),
line_program_files: line_program_files.clone(),
};
Expand Down
10 changes: 5 additions & 5 deletions src/write/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ pub trait Writer {
/// The write must not extend past the current section length.
fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()>;

/// Write an address that is relative to the given symbol.
/// Write an address.
///
/// If the writer supports relocations, then it must provide its own implementation
/// of this method.
fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
match address {
Address::Absolute(val) => self.write_word(val, size),
Address::Relative { .. } => Err(Error::InvalidAddress),
Address::Constant(val) => self.write_word(val, size),
Address::Symbol { .. } => Err(Error::InvalidAddress),
}
}

Expand Down Expand Up @@ -236,11 +236,11 @@ mod tests {
#[allow(clippy::cyclomatic_complexity)]
fn test_writer() {
let mut w = write::EndianVec::new(LittleEndian);
w.write_address(Address::Absolute(0x1122_3344), 4).unwrap();
w.write_address(Address::Constant(0x1122_3344), 4).unwrap();
assert_eq!(w.slice(), &[0x44, 0x33, 0x22, 0x11]);
assert_eq!(
w.write_address(
Address::Relative {
Address::Symbol {
symbol: 0,
addend: 0
},
Expand Down
4 changes: 2 additions & 2 deletions tests/convert_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn test_convert_debug_info() {
..Default::default()
};

let mut dwarf = write::Dwarf::from(&dwarf, &|address| Some(Address::Absolute(address)))
let mut dwarf = write::Dwarf::from(&dwarf, &|address| Some(Address::Constant(address)))
.expect("Should convert DWARF information");

assert_eq!(dwarf.units.count(), 23);
Expand Down Expand Up @@ -101,7 +101,7 @@ fn test_convert_debug_info() {
..Default::default()
};

let dwarf = write::Dwarf::from(&dwarf, &|address| Some(Address::Absolute(address)))
let dwarf = write::Dwarf::from(&dwarf, &|address| Some(Address::Constant(address)))
.expect("Should convert DWARF information");

assert_eq!(dwarf.units.count(), 23);
Expand Down

0 comments on commit 998c21c

Please sign in to comment.