| ... | ... | @@ -17,12 +17,15 @@ pub const DynLib = struct { |
| 17 | 17 | DlDynLib, |
| 18 | 18 | .windows => WindowsDynLib, |
| 19 | 19 | .macos, .tvos, .watchos, .ios, .visionos, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib, |
| 20 | | else => @compileError("unsupported platform"), |
| 20 | else => struct { |
| 21 | const open = @compileError("unsupported platform"); |
| 22 | const openZ = @compileError("unsupported platform"); |
| 23 | }, |
| 21 | 24 | }; |
| 22 | 25 | |
| 23 | 26 | inner: InnerType, |
| 24 | 27 | |
| 25 | | pub const Error = ElfDynLib.Error || DlDynLib.Error || WindowsDynLib.Error; |
| 28 | pub const Error = ElfDynLibError || DlDynLibError || WindowsDynLibError; |
| 26 | 29 | |
| 27 | 30 | /// Trusts the file. Malicious file will be able to execute arbitrary code. |
| 28 | 31 | pub fn open(path: []const u8) Error!DynLib { |
| ... | ... | @@ -122,6 +125,18 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) error{InvalidExe}!LinkMap.Iterator { |
| 122 | 125 | return .{ .current = link_map_ptr }; |
| 123 | 126 | } |
| 124 | 127 | |
| 128 | /// Separated to avoid referencing `ElfDynLib`, because its field types may not |
| 129 | /// be valid on other targets. |
| 130 | const ElfDynLibError = error{ |
| 131 | FileTooBig, |
| 132 | NotElfFile, |
| 133 | NotDynamicLibrary, |
| 134 | MissingDynamicLinkingInformation, |
| 135 | ElfStringSectionNotFound, |
| 136 | ElfSymSectionNotFound, |
| 137 | ElfHashTableNotFound, |
| 138 | } || posix.OpenError || posix.MMapError; |
| 139 | |
| 125 | 140 | pub const ElfDynLib = struct { |
| 126 | 141 | strings: [*:0]u8, |
| 127 | 142 | syms: [*]elf.Sym, |
| ... | ... | @@ -130,15 +145,7 @@ pub const ElfDynLib = struct { |
| 130 | 145 | verdef: ?*elf.Verdef, |
| 131 | 146 | memory: []align(mem.page_size) u8, |
| 132 | 147 | |
| 133 | | pub const Error = error{ |
| 134 | | FileTooBig, |
| 135 | | NotElfFile, |
| 136 | | NotDynamicLibrary, |
| 137 | | MissingDynamicLinkingInformation, |
| 138 | | ElfStringSectionNotFound, |
| 139 | | ElfSymSectionNotFound, |
| 140 | | ElfHashTableNotFound, |
| 141 | | } || posix.OpenError || posix.MMapError; |
| 148 | pub const Error = ElfDynLibError; |
| 142 | 149 | |
| 143 | 150 | /// Trusts the file. Malicious file will be able to execute arbitrary code. |
| 144 | 151 | pub fn open(path: []const u8) Error!ElfDynLib { |
| ... | ... | @@ -350,11 +357,15 @@ test "ElfDynLib" { |
| 350 | 357 | try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so")); |
| 351 | 358 | } |
| 352 | 359 | |
| 360 | /// Separated to avoid referencing `WindowsDynLib`, because its field types may not |
| 361 | /// be valid on other targets. |
| 362 | const WindowsDynLibError = error{ |
| 363 | FileNotFound, |
| 364 | InvalidPath, |
| 365 | } || windows.LoadLibraryError; |
| 366 | |
| 353 | 367 | pub const WindowsDynLib = struct { |
| 354 | | pub const Error = error{ |
| 355 | | FileNotFound, |
| 356 | | InvalidPath, |
| 357 | | } || windows.LoadLibraryError; |
| 368 | pub const Error = WindowsDynLibError; |
| 358 | 369 | |
| 359 | 370 | dll: windows.HMODULE, |
| 360 | 371 | |
| ... | ... | @@ -413,8 +424,12 @@ pub const WindowsDynLib = struct { |
| 413 | 424 | } |
| 414 | 425 | }; |
| 415 | 426 | |
| 427 | /// Separated to avoid referencing `DlDynLib`, because its field types may not |
| 428 | /// be valid on other targets. |
| 429 | const DlDynLibError = error{ FileNotFound, NameTooLong }; |
| 430 | |
| 416 | 431 | pub const DlDynLib = struct { |
| 417 | | pub const Error = error{ FileNotFound, NameTooLong }; |
| 432 | pub const Error = DlDynLibError; |
| 418 | 433 | |
| 419 | 434 | handle: *anyopaque, |
| 420 | 435 | |