Skip to content

Commit

Permalink
Merge pull request #12 from dpaoliello/simplify
Browse files Browse the repository at this point in the history
Removed unused options (deterministic, symbol table)
  • Loading branch information
bjorn3 authored Apr 9, 2024
2 parents 3c88e2c + a0ed9ed commit 381b2aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ar_archive_writer"
version = "0.1.5"
version = "0.2.0"
edition = "2021"
license = "Apache-2.0 WITH LLVM-exception"
description = "A writer for object file ar archives"
Expand Down
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# A writer for object file ar archives

This is based on commit [8ef3e895a](https://github.com/llvm/llvm-project/tree/3d3ef9d073e1e27ea57480b371b7f5a9f5642ed2) (15.0.0-rc3) of LLVM's archive writer.
This is a Rust port of LLVM's archive writer (`ArchiveWriter.cpp`):
* Based on commit [8ef3e895a](https://github.com/llvm/llvm-project/tree/3d3ef9d073e1e27ea57480b371b7f5a9f5642ed2) (15.0.0-rc3).
* With the following options removed:
* Deterministic: always enabled.
* Symbol tables: always enabled.

## License

Expand Down
68 changes: 14 additions & 54 deletions src/archive_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ fn compute_string_table(names: &[u8]) -> MemberData<'_> {
}
}

fn now(deterministic: bool) -> u64 {
if !deterministic {
todo!("non deterministic mode is not yet supported"); // FIXME
}
const fn now() -> u64 {
0
}

Expand Down Expand Up @@ -293,7 +290,6 @@ fn compute_symbol_table_size_and_pad(
fn write_symbol_table_header<W: Write + Seek>(
w: &mut W,
kind: ArchiveKind,
deterministic: bool,
size: u64,
prev_member_offset: u64,
) -> io::Result<()> {
Expand All @@ -304,29 +300,18 @@ fn write_symbol_table_header<W: Write + Seek>(
"__.SYMDEF"
};
let pos = w.stream_position()?;
print_bsd_member_header(w, pos, name, now(deterministic), 0, 0, 0, size)
print_bsd_member_header(w, pos, name, now(), 0, 0, 0, size)
} else if is_aix_big_archive(kind) {
print_big_archive_member_header(
w,
"",
now(deterministic),
0,
0,
0,
size,
prev_member_offset,
0,
)
print_big_archive_member_header(w, "", now(), 0, 0, 0, size, prev_member_offset, 0)
} else {
let name = if is_64bit_kind(kind) { "/SYM64" } else { "" };
print_gnu_small_member_header(w, name.to_string(), now(deterministic), 0, 0, 0, size)
print_gnu_small_member_header(w, name.to_string(), now(), 0, 0, 0, size)
}
}

fn write_symbol_table<W: Write + Seek>(
w: &mut W,
kind: ArchiveKind,
deterministic: bool,
members: &[MemberData<'_>],
string_table: &[u8],
prev_member_offset: u64,
Expand All @@ -341,7 +326,7 @@ fn write_symbol_table<W: Write + Seek>(

let offset_size = if is_64bit_kind(kind) { 8 } else { 4 };
let (size, pad) = compute_symbol_table_size_and_pad(kind, num_syms, offset_size, string_table);
write_symbol_table_header(w, kind, deterministic, size, prev_member_offset)?;
write_symbol_table_header(w, kind, size, prev_member_offset)?;

let mut pos = if is_aix_big_archive(kind) {
u64::try_from(std::mem::size_of::<big_archive::FixLenHdr>()).unwrap()
Expand Down Expand Up @@ -427,8 +412,6 @@ fn compute_member_data<'a, S: Write + Seek>(
sym_names: &mut Cursor<Vec<u8>>,
kind: ArchiveKind,
thin: bool,
deterministic: bool,
need_symbols: bool,
new_members: &'a [NewArchiveMember<'a>],
) -> io::Result<Vec<MemberData<'a>>> {
const PADDING_DATA: &[u8; 8] = &[b'\n'; 8];
Expand Down Expand Up @@ -490,7 +473,7 @@ fn compute_member_data<'a, S: Write + Seek>(
// See also the functions that handle the lookup:
// in lldb: ObjectContainerBSDArchive::Archive::FindObject()
// in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
let unique_timestamps = deterministic && is_darwin(kind);
let unique_timestamps = is_darwin(kind);
let mut filename_count = HashMap::new();
if unique_timestamps {
for m in new_members {
Expand Down Expand Up @@ -569,11 +552,7 @@ fn compute_member_data<'a, S: Write + Seek>(
)?;
}

let symbols = if need_symbols {
write_symbols(data, m.get_symbols, sym_names, &mut has_object)?
} else {
vec![]
};
let symbols = write_symbols(data, m.get_symbols, sym_names, &mut has_object)?;

pos += u64::try_from(header.len() + data.len() + padding.len()).unwrap();
ret.push(MemberData {
Expand All @@ -597,9 +576,7 @@ fn compute_member_data<'a, S: Write + Seek>(
pub fn write_archive_to_stream<W: Write + Seek>(
w: &mut W,
new_members: &[NewArchiveMember<'_>],
write_symtab: bool,
mut kind: ArchiveKind,
deterministic: bool,
thin: bool,
) -> io::Result<()> {
assert!(
Expand All @@ -610,15 +587,7 @@ pub fn write_archive_to_stream<W: Write + Seek>(
let mut sym_names = Cursor::new(Vec::new());
let mut string_table = Cursor::new(Vec::new());

let mut data = compute_member_data(
&mut string_table,
&mut sym_names,
kind,
thin,
deterministic,
write_symtab,
new_members,
)?;
let mut data = compute_member_data(&mut string_table, &mut sym_names, kind, thin, new_members)?;

let sym_names = sym_names.into_inner();

Expand Down Expand Up @@ -646,13 +615,13 @@ pub fn write_archive_to_stream<W: Write + Seek>(

// The symbol table is put at the end of the big archive file. The symbol
// table is at the start of the archive file for other archive formats.
if write_symtab && !is_aix_big_archive(kind) {
if !is_aix_big_archive(kind) {
// We assume 32-bit offsets to see if 32-bit symbols are possible or not.
let (symtab_size, _pad) = compute_symbol_table_size_and_pad(kind, num_syms, 4, &sym_names);
last_member_header_offset += {
// FIXME avoid allocating memory here
let mut tmp = Cursor::new(vec![]);
write_symbol_table_header(&mut tmp, kind, deterministic, symtab_size, 0).unwrap();
write_symbol_table_header(&mut tmp, kind, symtab_size, 0).unwrap();
u64::try_from(tmp.into_inner().len()).unwrap()
} + symtab_size;

Expand Down Expand Up @@ -687,9 +656,7 @@ pub fn write_archive_to_stream<W: Write + Seek>(
}

if !is_aix_big_archive(kind) {
if write_symtab {
write_symbol_table(w, kind, deterministic, &data, &sym_names, 0)?;
}
write_symbol_table(w, kind, &data, &sym_names, 0)?;

for m in data {
w.write_all(&m.header)?;
Expand Down Expand Up @@ -723,7 +690,7 @@ pub fn write_archive_to_stream<W: Write + Seek>(
let member_table_size =
u64::try_from(20 + 20 * member_offsets.len() + member_table_name_str_tbl_size).unwrap();

let global_symbol_offset = if write_symtab && num_syms > 0 {
let global_symbol_offset = if num_syms > 0 {
last_member_end_offset
+ align_to(
u64::try_from(std::mem::size_of::<big_archive::BigArMemHdrType>()).unwrap()
Expand Down Expand Up @@ -817,15 +784,8 @@ pub fn write_archive_to_stream<W: Write + Seek>(
w.write_all(&[0])?;
}

if write_symtab && num_syms > 0 {
write_symbol_table(
w,
kind,
deterministic,
&data,
&sym_names,
last_member_end_offset,
)?;
if num_syms > 0 {
write_symbol_table(w, kind, &data, &sym_names, last_member_end_offset)?;
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,8 @@ pub fn create_archive_with_ar_archive_writer<'a>(
})
.collect::<Vec<_>>();
let mut output_bytes = Cursor::new(Vec::new());
ar_archive_writer::write_archive_to_stream(
&mut output_bytes,
&members,
true,
archive_kind,
true,
false,
)
.unwrap();
ar_archive_writer::write_archive_to_stream(&mut output_bytes, &members, archive_kind, false)
.unwrap();

let output_archive_bytes = output_bytes.into_inner();
let ar_archive_writer_file_path = tmpdir.join("output_ar_archive_writer.a");
Expand Down

0 comments on commit 381b2aa

Please sign in to comment.