authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-04 00:21:38-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-04 00:21:38-07:00
logf01cb8cc16bd048adefdec5a426d5ef33dff9168
tree61c62a9f732fcf0a0ec1c01dc9ae4d807b3a1aa2
parent5687323cd2a759dfdf9533e46e3de8cddb1b55b7
parent887eac0219345763f1ae9c8d9efad6950f6bbfe6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2998 from daurnimator/return-elf

std: return Elf object from constructors instead of filling in pointer

2 files changed, 7 insertions(+), 11 deletions(-)

std/debug.zig+1-2
...@@ -1024,8 +1024,7 @@ pub fn openElfDebugInfo(...@@ -1024,8 +1024,7 @@ pub fn openElfDebugInfo(
1024 elf_seekable_stream: *DwarfSeekableStream,1024 elf_seekable_stream: *DwarfSeekableStream,
1025 elf_in_stream: *DwarfInStream,1025 elf_in_stream: *DwarfInStream,
1026) !DwarfInfo {1026) !DwarfInfo {
1027 var efile: elf.Elf = undefined;1027 var efile = try elf.Elf.openStream(allocator, elf_seekable_stream, elf_in_stream);
1028 try efile.openStream(allocator, elf_seekable_stream, elf_in_stream);
1029 errdefer efile.close();1028 errdefer efile.close();
10301029
1031 var di = DwarfInfo{1030 var di = DwarfInfo{
std/elf.zig+6-9
...@@ -356,7 +356,6 @@ pub const SectionHeader = struct {...@@ -356,7 +356,6 @@ pub const SectionHeader = struct {
356pub const Elf = struct {356pub const Elf = struct {
357 seekable_stream: *io.SeekableStream(anyerror, anyerror),357 seekable_stream: *io.SeekableStream(anyerror, anyerror),
358 in_stream: *io.InStream(anyerror),358 in_stream: *io.InStream(anyerror),
359 auto_close_stream: bool,
360 is_64: bool,359 is_64: bool,
361 endian: builtin.Endian,360 endian: builtin.Endian,
362 file_type: FileType,361 file_type: FileType,
...@@ -368,25 +367,23 @@ pub const Elf = struct {...@@ -368,25 +367,23 @@ pub const Elf = struct {
368 string_section: *SectionHeader,367 string_section: *SectionHeader,
369 section_headers: []SectionHeader,368 section_headers: []SectionHeader,
370 allocator: *mem.Allocator,369 allocator: *mem.Allocator,
371 prealloc_file: File,
372370
373 /// Call close when done.371 /// Call close when done.
374 pub fn openPath(elf: *Elf, allocator: *mem.Allocator, path: []const u8) !void {372 pub fn openPath(allocator: *mem.Allocator, path: []const u8) !Elf {
375 @compileError("TODO implement");373 @compileError("TODO implement");
376 }374 }
377375
378 /// Call close when done.376 /// Call close when done.
379 pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: File) !void {377 pub fn openFile(allocator: *mem.Allocator, file: File) !Elf {
380 @compileError("TODO implement");378 @compileError("TODO implement");
381 }379 }
382380
383 pub fn openStream(381 pub fn openStream(
384 elf: *Elf,
385 allocator: *mem.Allocator,382 allocator: *mem.Allocator,
386 seekable_stream: *io.SeekableStream(anyerror, anyerror),383 seekable_stream: *io.SeekableStream(anyerror, anyerror),
387 in: *io.InStream(anyerror),384 in: *io.InStream(anyerror),
388 ) !void {385 ) !Elf {
389 elf.auto_close_stream = false;386 var elf: Elf = undefined;
390 elf.allocator = allocator;387 elf.allocator = allocator;
391 elf.seekable_stream = seekable_stream;388 elf.seekable_stream = seekable_stream;
392 elf.in_stream = in;389 elf.in_stream = in;
...@@ -523,12 +520,12 @@ pub const Elf = struct {...@@ -523,12 +520,12 @@ pub const Elf = struct {
523 // not a string table520 // not a string table
524 return error.InvalidFormat;521 return error.InvalidFormat;
525 }522 }
523
524 return elf;
526 }525 }
527526
528 pub fn close(elf: *Elf) void {527 pub fn close(elf: *Elf) void {
529 elf.allocator.free(elf.section_headers);528 elf.allocator.free(elf.section_headers);
530
531 if (elf.auto_close_stream) elf.prealloc_file.close();
532 }529 }
533530
534 pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {531 pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {