aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-08-11 06:19:10 -0400
committerGitHub <noreply@github.com>2022-08-11 06:19:10 -0400
commit44a6172edbf6c99f4ebfeabb530756ed2a40c3c2 (patch)
treefb158886b71bc3e1a19d09aea13b4e3d19b164ea
parent9f1f60fd4311b37493a5297ccaac167d0270f521 (diff)
parentfa620ef710bbbf7f642fd1d6d24a188d02902988 (diff)
downloadzig-44a6172edbf6c99f4ebfeabb530756ed2a40c3c2.tar.gz
zig-44a6172edbf6c99f4ebfeabb530756ed2a40c3c2.zip
Merge pull request #12405 from ziglang/macho-aligned-ptrs
link.MachO: use accurate alignment attribute on pointers
-rw-r--r--lib/std/array_list.zig63
-rw-r--r--src/link/MachO.zig6
-rw-r--r--src/link/MachO/Atom.zig2
-rw-r--r--src/link/MachO/Object.zig24
4 files changed, 79 insertions, 16 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index 655bdeaa42..a8c53c3142 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -221,6 +221,30 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
mem.copy(T, self.items[old_len..], items);
}
+ /// Append an unaligned slice of items to the list. Allocates more
+ /// memory as necessary. Only call this function if calling
+ /// `appendSlice` instead would be a compile error.
+ pub fn appendUnalignedSlice(self: *Self, items: []align(1) const T) Allocator.Error!void {
+ try self.ensureUnusedCapacity(items.len);
+ self.appendUnalignedSliceAssumeCapacity(items);
+ }
+
+ /// Append the slice of items to the list, asserting the capacity is already
+ /// enough to store the new items. **Does not** invalidate pointers.
+ /// Only call this function if calling `appendSliceAssumeCapacity` instead
+ /// would be a compile error.
+ pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void {
+ const old_len = self.items.len;
+ const new_len = old_len + items.len;
+ assert(new_len <= self.capacity);
+ self.items.len = new_len;
+ @memcpy(
+ @ptrCast([*]align(@alignOf(T)) u8, self.items.ptr + old_len),
+ @ptrCast([*]const u8, items.ptr),
+ items.len * @sizeOf(T),
+ );
+ }
+
pub const Writer = if (T != u8)
@compileError("The Writer interface is only defined for ArrayList(u8) " ++
"but the given type is ArrayList(" ++ @typeName(T) ++ ")")
@@ -592,6 +616,29 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
mem.copy(T, self.items[old_len..], items);
}
+ /// Append the slice of items to the list. Allocates more
+ /// memory as necessary. Only call this function if a call to `appendSlice` instead would
+ /// be a compile error.
+ pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void {
+ try self.ensureUnusedCapacity(allocator, items.len);
+ self.appendUnalignedSliceAssumeCapacity(items);
+ }
+
+ /// Append an unaligned slice of items to the list, asserting the capacity is enough
+ /// to store the new items. Only call this function if a call to `appendSliceAssumeCapacity`
+ /// instead would be a compile error.
+ pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void {
+ const old_len = self.items.len;
+ const new_len = old_len + items.len;
+ assert(new_len <= self.capacity);
+ self.items.len = new_len;
+ @memcpy(
+ @ptrCast([*]align(@alignOf(T)) u8, self.items.ptr + old_len),
+ @ptrCast([*]const u8, items.ptr),
+ items.len * @sizeOf(T),
+ );
+ }
+
pub const WriterContext = struct {
self: *Self,
allocator: Allocator,
@@ -899,6 +946,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
try testing.expect(list.pop() == 1);
try testing.expect(list.items.len == 9);
+ var unaligned: [3]i32 align(1) = [_]i32{ 4, 5, 6 };
+ list.appendUnalignedSlice(&unaligned) catch unreachable;
+ try testing.expect(list.items.len == 12);
+ try testing.expect(list.pop() == 6);
+ try testing.expect(list.pop() == 5);
+ try testing.expect(list.pop() == 4);
+ try testing.expect(list.items.len == 9);
+
list.appendSlice(&[_]i32{}) catch unreachable;
try testing.expect(list.items.len == 9);
@@ -941,6 +996,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
try testing.expect(list.pop() == 1);
try testing.expect(list.items.len == 9);
+ var unaligned: [3]i32 align(1) = [_]i32{ 4, 5, 6 };
+ list.appendUnalignedSlice(a, &unaligned) catch unreachable;
+ try testing.expect(list.items.len == 12);
+ try testing.expect(list.pop() == 6);
+ try testing.expect(list.pop() == 5);
+ try testing.expect(list.pop() == 4);
+ try testing.expect(list.items.len == 9);
+
list.appendSlice(a, &[_]i32{}) catch unreachable;
try testing.expect(list.items.len == 9);
diff --git a/src/link/MachO.zig b/src/link/MachO.zig
index db207af5f5..3da086a382 100644
--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -5315,10 +5315,10 @@ fn writeFunctionStarts(self: *MachO, ncmds: *u32, lc_writer: anytype) !void {
}
fn filterDataInCode(
- dices: []const macho.data_in_code_entry,
+ dices: []align(1) const macho.data_in_code_entry,
start_addr: u64,
end_addr: u64,
-) []const macho.data_in_code_entry {
+) []align(1) const macho.data_in_code_entry {
const Predicate = struct {
addr: u64,
@@ -5825,7 +5825,7 @@ pub fn getEntryPoint(self: MachO) error{MissingMainEntrypoint}!SymbolWithLoc {
return global;
}
-pub fn findFirst(comptime T: type, haystack: []const T, start: usize, predicate: anytype) usize {
+pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, predicate: anytype) usize {
if (!@hasDecl(@TypeOf(predicate), "predicate"))
@compileError("Predicate is required to define fn predicate(@This(), T) bool");
diff --git a/src/link/MachO/Atom.zig b/src/link/MachO/Atom.zig
index 4871276f3c..dd818ea936 100644
--- a/src/link/MachO/Atom.zig
+++ b/src/link/MachO/Atom.zig
@@ -218,7 +218,7 @@ const RelocContext = struct {
base_offset: i32 = 0,
};
-pub fn parseRelocs(self: *Atom, relocs: []const macho.relocation_info, context: RelocContext) !void {
+pub fn parseRelocs(self: *Atom, relocs: []align(1) const macho.relocation_info, context: RelocContext) !void {
const tracy = trace(@src());
defer tracy.end();
diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig
index 098917e81f..935183bbc6 100644
--- a/src/link/MachO/Object.zig
+++ b/src/link/MachO/Object.zig
@@ -24,7 +24,7 @@ mtime: u64,
contents: []align(@alignOf(u64)) const u8,
header: macho.mach_header_64 = undefined,
-in_symtab: []const macho.nlist_64 = undefined,
+in_symtab: []align(1) const macho.nlist_64 = undefined,
in_strtab: []const u8 = undefined,
symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{},
@@ -100,12 +100,12 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
.SYMTAB => {
const symtab = cmd.cast(macho.symtab_command).?;
// Sadly, SYMTAB may be at an unaligned offset within the object file.
- self.in_symtab = @alignCast(@alignOf(macho.nlist_64), @ptrCast(
+ self.in_symtab = @ptrCast(
[*]align(1) const macho.nlist_64,
self.contents.ptr + symtab.symoff,
- ))[0..symtab.nsyms];
+ )[0..symtab.nsyms];
self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];
- try self.symtab.appendSlice(allocator, self.in_symtab);
+ try self.symtab.appendUnalignedSlice(allocator, self.in_symtab);
},
else => {},
}
@@ -197,10 +197,10 @@ fn filterSymbolsByAddress(
}
fn filterRelocs(
- relocs: []const macho.relocation_info,
+ relocs: []align(1) const macho.relocation_info,
start_addr: u64,
end_addr: u64,
-) []const macho.relocation_info {
+) []align(1) const macho.relocation_info {
const Predicate = struct {
addr: u64,
@@ -303,10 +303,10 @@ pub fn splitIntoAtomsOneShot(self: *Object, macho_file: *MachO, object_id: u32)
const code: ?[]const u8 = if (!sect.isZerofill()) try self.getSectionContents(sect) else null;
// Read section's list of relocations
- const relocs = @alignCast(@alignOf(macho.relocation_info), @ptrCast(
+ const relocs = @ptrCast(
[*]align(1) const macho.relocation_info,
self.contents.ptr + sect.reloff,
- ))[0..sect.nreloc];
+ )[0..sect.nreloc];
// Symbols within this section only.
const filtered_syms = filterSymbolsByAddress(
@@ -473,7 +473,7 @@ fn createAtomFromSubsection(
size: u64,
alignment: u32,
code: ?[]const u8,
- relocs: []const macho.relocation_info,
+ relocs: []align(1) const macho.relocation_info,
indexes: []const SymbolAtIndex,
match: u8,
sect: macho.section_64,
@@ -539,7 +539,7 @@ pub fn getSourceSection(self: Object, index: u16) macho.section_64 {
return self.sections.items[index];
}
-pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
+pub fn parseDataInCode(self: Object) ?[]align(1) const macho.data_in_code_entry {
var it = LoadCommandIterator{
.ncmds = self.header.ncmds,
.buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],
@@ -549,10 +549,10 @@ pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
.DATA_IN_CODE => {
const dice = cmd.cast(macho.linkedit_data_command).?;
const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry));
- return @alignCast(@alignOf(macho.data_in_code_entry), @ptrCast(
+ return @ptrCast(
[*]align(1) const macho.data_in_code_entry,
self.contents.ptr + dice.dataoff,
- ))[0..ndice];
+ )[0..ndice];
},
else => {},
}