Author SHA1 Message Date
Nikita Smith f792da5d31 manifest... 2024-10-18 16:53:30 -07:00
Ryan Fleury 1344270b5a adjust versions 2024-10-18 16:40:25 -07:00
Ryan Fleury c03844fb1a fix build, make icon unique to raddbg 2024-10-18 16:32:49 -07:00
Nikita Smith d04e86c007 rename merged manifest 2024-10-18 16:23:51 -07:00
Ryan Fleury 669574df5a fix incorrect ordering of inline sites & concrete frames in call stack view 2024-10-18 16:07:25 -07:00
Nikita Smith 3aa4b2ea03 fix udt resolution & emit correct null type 2024-10-18 16:01:17 -07:00
Nikita Smith 2f5e55d52f switch check typo 2024-10-18 16:01:17 -07:00
Nikita Smith 14ad67d59a assign correct file path node to source file
* disabled debug check for sortness too while I'm here
2024-10-18 16:01:17 -07:00
Nikita Smith b2bdb893a0 print file path node idx 2024-10-18 16:01:17 -07:00
Ryan Fleury 98dfedfff5 limit the amount of line info visualization voff gathering; otherwise we can just spend tons of time gathering inline site voffs forever... 2024-10-18 15:16:34 -07:00
Ryan Fleury 1f633a029c skip redundant expansion tasks in eval viz block tree build 2024-10-18 13:58:24 -07:00
Ryan Fleury b3d6b2913e fix enum value committing 2024-10-18 13:47:51 -07:00
Ryan Fleury 53c624a27c respect view rule radix in enum evaluations 2024-10-18 13:37:14 -07:00
Ryan Fleury 1865c33704 specify collection qualifier for collection tabs; do not resolve to anything else 2024-10-18 13:32:29 -07:00
Ryan Fleury e17a089564 fix incorrect 0 member (should be nil) 2024-10-18 12:14:40 -07:00
Ryan Fleury eb9212b6f7 fix string cmd query params 2024-10-18 12:00:13 -07:00
Nikita Smith 0ac090cab2 delete unused files 2024-10-18 11:07:00 -07:00
Nikita Smith 7827e1b1c7 fix bounds check condition in assert 2024-10-18 11:07:00 -07:00
Nikita Smith 36f3924160 don't allocate .reloc section when there are no pages 2024-10-18 11:05:51 -07:00
Nikita Smith 00dd4ad6ec check if subsystem was specified 2024-10-18 11:05:01 -07:00
Nikita Smith 7f7f7f2873 reserve space for null string 2024-10-18 11:03:46 -07:00
Nikita Smith c8a5fc4806 Changed debug directory parser to handle multiple entries. Added
parser for RDI debug info entry. Changed debug info lookup order so
debugger tries to load path specified in exe first and if debug info
is missing then debugger will do heuristic searches.
2024-10-17 18:51:35 -07:00
Nikita Smith 652dcafbbf demon helper for reading cstrings 2024-10-17 18:46:59 -07:00
Ryan Fleury 72f987533c Merge branch 'dev' of https://github.com/EpicGames/raddebugger into dev 2024-10-17 16:50:50 -07:00
Ryan Fleury 5fd835c4d4 note 2024-10-17 16:50:25 -07:00
Nikita Smith c50d9276ba build inline lines accels 2024-10-17 16:25:54 -07:00
Nikita Smith 1214cb0968 dumper for CodeView inline sites 2024-10-17 16:25:54 -07:00
Nikita Smith 3099759c0b stringizers for inline site range kind 2024-10-17 16:25:54 -07:00
Nikita Smith 95d31f5608 fix off-by-one in inline site decode 2024-10-17 16:25:54 -07:00
Ryan Fleury 687323fc2e fix soft-halt with thread freezes; fix disabling/tooltips of top-bar controls 2024-10-17 16:12:31 -07:00
Ryan Fleury 51dce36ede notes 2024-10-17 14:51:41 -07:00
Ryan Fleury b61f4d550f Merge branch 'dev' of https://github.com/EpicGames/raddebugger into dev 2024-10-17 14:51:28 -07:00
Ryan Fleury d8d8762869 adjust entity rows in watch views to use single-click for select, double-click for their primary operation, and add in operation for breakpoints/watch-pins 2024-10-17 14:51:07 -07:00
Nikita Smith d2db4542cb check type server signature against signature in obj 2024-10-17 14:05:34 -07:00
Ryan Fleury 7adf08cc7b parameterize string eval commit path by whether or not the surrounding ui expects the string to be manually escaped or not by the user (it is not in the case of things like the meta-entity views) 2024-10-17 12:57:53 -07:00
Ryan Fleury a202999655 fix over-ambitious thread selection in frontend 2024-10-17 12:43:32 -07:00
Ryan Fleury ae3a43c694 fix file-path-parameterized commands w/ ipc 2024-10-17 12:32:20 -07:00
Ryan Fleury 2f7434cd92 always use pending-file when doing a file switch 2024-10-17 12:29:06 -07:00
Ryan Fleury b02c67bfa6 close stdout/stderr/stdin handles 2024-10-17 12:20:03 -07:00
Ryan Fleury 992be37801 fix unclamped-by-line token ranges in code slice widget mouse -> token path 2024-10-17 12:13:32 -07:00
Ryan Fleury 4727a01b9f fix kill/halt button & text 2024-10-17 12:01:02 -07:00
Ryan Fleury e376ad8113 stdout/stderr/stdin file path overrides for targets 2024-10-17 11:25:47 -07:00
Ryan Fleury 23ad094a68 allow escaped-identifier lookup for type names 2024-10-17 10:46:14 -07:00
Nikita Smith 2b407b5861 changed write file to accept 1MiB blocks to improve file write speed on windows 2024-10-16 22:27:46 -07:00
Nikita Smith 79390f4a95 align msf pages on page size 2024-10-16 22:27:46 -07:00
Nikita Smith aedfb556be fix aligned arena push 2024-10-16 22:27:46 -07:00
Ryan Fleury 78039c86a0 include no_addr in view rule system for docs/inheritance rules 2024-10-16 15:41:21 -07:00
Ryan Fleury 186e8cbabe shift disasm view to rounding/sizing equally 2024-10-16 14:58:13 -07:00
Ryan Fleury 5ec0a0e406 fix incorrect inline site baking 2024-10-16 14:29:08 -07:00
Martins MozeikoandRyan Fleury 9c016004c2 Setup symbol path in win32 exception handler to the .exe folder
Otherwise it was trying lookup .pdb files only in the current folder,
as that is how .exe's are built (/pdbaltpath). This change now makes
exception handler to load .pdb from the same folder as where .exe is.
2024-10-16 14:13:26 -07:00
Ryan Fleury 9c1f8ef37b file name disambiguation for tab titles 2024-10-16 14:12:21 -07:00
Ryan Fleury 938af75efb normalize paths when doing file-path <-> eval string 2024-10-16 13:33:47 -07:00
Ryan Fleury 38173a7154 adjust pointer visualization; don't follow 0 pointers, go back to showing address first; adjust find-code-location snapping rule to only require a disasm snap / focus if no file location is specified 2024-10-16 13:23:10 -07:00
Ryan Fleury 74ee3b9850 option to rdi_from_pdb for deterministic generation; omits non-intrinsic, build-dependent information 2024-10-16 12:47:10 -07:00
Ryan Fleury 2edb65b5bd fix remaining non-determinism in pdb -> rdi generation! 2024-10-16 12:07:02 -07:00
Nikita Smith 9e6aa8506f README.md tweaks 2024-10-16 10:48:51 -07:00
Nikita Smith 648fa7cdab update hash in config 2024-10-16 10:23:53 -07:00
Nikita Smith 6612f4a32d hash image only when we have debug info building 2024-10-16 10:15:16 -07:00
Ryan Fleury e65bc121ed more work on pdb -> rdi determinism; almost done hitting all of the issues... 2024-10-16 08:52:27 -07:00
Ryan Fleury e1575f815c Merge branch 'dev' of https://github.com/EpicGames/raddebugger into dev 2024-10-15 18:05:14 -07:00
Ryan Fleury 0b50d8718e oops - reenable 2024-10-15 18:05:05 -07:00
Ryan Fleury 4671458e8e further debugging / logging / work on tester, trynig to get to the bottom of nondeterministic rdi generation 2024-10-15 17:41:18 -07:00
Nikita Smith bb86072dfa updated git actions 2024-10-15 17:32:21 -07:00
Nikita Smith d5988d0066 updated README 2024-10-15 17:26:50 -07:00
Nikita Smith 932df7bf68 linker 2024-10-15 17:25:22 -07:00
Nikita Smith 0cf553e39a minor fixes 2024-10-15 17:24:29 -07:00
Nikita Smith 1bcd66329f factored parser code out of pdb 2024-10-15 16:28:51 -07:00
Nikita Smith c3e2e2e91b factored parser code out of msf 2024-10-15 16:28:47 -07:00
Nikita Smith c34d7e2062 added missing LF_ENDPRECOMP type, fixed case for S_OBJNAME symbol,
merged leaf id kinds into main leaf enum, renamed public flags enum,
removed type dependency on PDB string table and PDB coff section array
2024-10-15 16:24:29 -07:00
Nikita Smith b1d6ebf77b added masm language enum and type kind count 2024-10-15 16:20:18 -07:00
Nikita Smith 9535652cfb define null path style 2024-10-15 16:19:27 -07:00
Nikita Smith 3ae1d8e401 bunch of tiny tweaks and changes from internal repo 2024-10-15 16:18:43 -07:00
Nikita Smith 7e98df22d7 temp arena change that lets us bypass memory decommits 2024-10-15 14:43:36 -07:00
Ryan Fleury 65b5176468 allocate extra slot for nil in global vmap baking; was causing overwrites of keys/markers before! 2024-10-15 12:42:43 -07:00
Ryan Fleury e61dfd793a stdout redirection support in os command line launch helper; do rdi dumps in determinism test 2024-10-15 12:20:25 -07:00
Ryan Fleury 97abec11a2 begin setting up tester for automated tests 2024-10-15 11:46:07 -07:00
Ryan Fleury b223a61cad tweaks & language fix 2024-10-15 10:17:37 -07:00
Ryan Fleury 94fb5056c2 resolve ptr-addition integer to value 2024-10-15 10:07:10 -07:00
Ryan Fleury 88dced7d2a notes 2024-10-15 09:00:54 -07:00
Ryan Fleury e92543d3f8 round to 4K rather than 16K boundaries for starting disasm address 2024-10-14 21:46:03 -07:00
Ryan Fleury 078cbf6afd fix incorrect auto disasm starting address 2024-10-14 20:56:25 -07:00
Martins MozeikoandRyan Fleury 3d34a26205 avx512 register support in codeview -> rdi conversion 2024-10-14 20:54:26 -07:00
189 changed files with 80369 additions and 2084 deletions
+2
View File
@@ -28,3 +28,5 @@ jobs:
call build rdi_from_pdb clang debug || exit /b 1
call build rdi_from_dwarf clang debug || exit /b 1
call build rdi_dump clang debug || exit /b 1
call build radlink msvc debug || exit /b 1
call build radlink clang debug || exit /b 1
+39
View File
@@ -158,6 +158,45 @@ like remote debugging, porting to different architectures, further improving
the debugger's features (like improving the visualization engine), and so on.
But for now, we're mostly focused on those first two phases.
---
# The RAD Linker
The RAD Linker is a new performance linker for generating x64 PE/COFF binaries. It is designed to be very fast when creating gigantic executables. It generates standard PDB files for debugging, but it can also optionally create RAD Debugger debug info too (useful for huge executables that otherwise create broken PDBs that overflow internal 32-bit tables).
The RAD Linker is primarily optimized to handle huge linking projects - in our test cases (where debug info is multiple gigabytes), we see 50% faster link times.
The command line syntax is fully compatible with MSVC and you can get a full list of implemented switches from `/help`.
Our current designed-for use case for the linker is to help with the compile-debug cycle of huge projects. We don't yet have support for dead-code-elimination or link-time-optimizations, but these features are on the road map.
By default, the RAD linker spawns as many threads as there are cores, so if you plan to run multiple linkers in parallel, you can limit the number of thread workers via `/rad_workers`.
We also have support for large memory pages, which, when enabled, reduce link time by
another 25%. To link with large pages, you need to explicitly request them via `/rad_large_pages`. Large pages are off by default, since Windows support for large pages is a bit buggy - we recommend they only be used in Docker or VM images where the environment is reset after each link. In a standard Windows environment, using large pages otherwise will fragment memory quickly forcing a reboot. We are working on a Linux port of the linker that will be able to build with large pages robustly.
## Short Term Roadmap
- Porting linker to Linux (for Windows executables, just running on Linux).
- Debug info features
- Get DWARF debug info converter up-and-running.
- Smooth out rough edges in RADDBGI builder.
- Improve build speed further (especially for tiny and mid sizes projects).
- Other features to come
- Dead-code-elimination via `/opt:ref`.
- Link Time Optimizations with the help of clang (we won't support LTCG from MSVC compiler since it is undocumented).
## To build the RAD Linker
- Setup development environment, [see](#Development-Setup-Instructions)
- Run `build radlink release` or if you have clang installed `build radlink release clang`. We favor latter option for better code generation.
If build was successful linker executable is placed in `build` folder under `radlink.exe`.
## Benchmarks
![AMD Ryzen Threadripper PRO 3995WX 64-Cores, 256 GiB RAM (Windows x64)](https://github.com/user-attachments/assets/39d95fdf-9f0b-45d3-9fb8-90d8ac624168)
---
## Top-Level Directory Descriptions
- `data`: Small binary files which are used when building, either to embed
+7 -3
View File
@@ -48,13 +48,14 @@ set cl_debug= call cl /Od /Ob1 /DBUILD_DEBUG=1 %cl_common% %auto_compile_fl
set cl_release= call cl /O2 /DBUILD_DEBUG=0 %cl_common% %auto_compile_flags%
set clang_debug= call clang -g -O0 -DBUILD_DEBUG=1 %clang_common% %auto_compile_flags%
set clang_release= call clang -g -O2 -DBUILD_DEBUG=0 %clang_common% %auto_compile_flags%
set cl_link= /link /MANIFEST:EMBED /INCREMENTAL:NO /natvis:"%~dp0\src\natvis\base.natvis" /pdbaltpath:%%%%_PDB%%%% logo.res
set clang_link= -fuse-ld=lld -Xlinker /MANIFEST:EMBED -Xlinker /natvis:"%~dp0\src\natvis\base.natvis" logo.res -Xlinker /pdbaltpath:%%%%_PDB%%%%
set cl_link= /link /MANIFEST:EMBED /INCREMENTAL:NO /natvis:"%~dp0\src\natvis\base.natvis" /pdbaltpath:%%%%_PDB%%%%
set clang_link= -fuse-ld=lld -Xlinker /MANIFEST:EMBED -Xlinker /natvis:"%~dp0\src\natvis\base.natvis" -Xlinker /pdbaltpath:%%%%_PDB%%%%
set cl_out= /out:
set clang_out= -o
:: --- Per-Build Settings -----------------------------------------------------
set link_dll=-DLL
set link_icon=logo.res
if "%msvc%"=="1" set only_compile=/c
if "%clang%"=="1" set only_compile=-c
if "%msvc%"=="1" set EHsc=/EHsc
@@ -99,12 +100,15 @@ if not "%no_meta%"=="1" (
:: --- Build Everything (@build_targets) --------------------------------------
pushd build
if "%raddbg%"=="1" set didbuild=1 && %compile% ..\src\raddbg\raddbg_main.c %compile_link% %out%raddbg.exe || exit /b 1
if "%raddbg%"=="1" set didbuild=1 && %compile% ..\src\raddbg\raddbg_main.c %compile_link% %link_icon% %out%raddbg.exe || exit /b 1
if "%radlink%"=="1" set didbuild=1 && %compile% ..\src\linker\lnk.c %compile_link% %out%radlink.exe || exit /b 1
if "%rdi_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_pdb\rdi_from_pdb_main.c %compile_link% %out%rdi_from_pdb.exe || exit /b 1
if "%rdi_from_dwarf%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_dwarf\rdi_from_dwarf.c %compile_link% %out%rdi_from_dwarf.exe || exit /b 1
if "%rdi_dump%"=="1" set didbuild=1 && %compile% ..\src\rdi_dump\rdi_dump_main.c %compile_link% %out%rdi_dump.exe || exit /b 1
if "%rdi_breakpad_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_breakpad_from_pdb\rdi_breakpad_from_pdb_main.c %compile_link% %out%rdi_breakpad_from_pdb.exe || exit /b 1
if "%tester%"=="1" set didbuild=1 && %compile% ..\src\tester\tester_main.c %compile_link% %out%tester.exe || exit /b 1
if "%ryan_scratch%"=="1" set didbuild=1 && %compile% ..\src\scratch\ryan_scratch.c %compile_link% %out%ryan_scratch.exe || exit /b 1
if "%parse_inline_sites%"=="1" set didbuild=1 && %compile% ..\src\scratch\parse_inline_sites.c %compile_link% %out%parse_inline_sites.exe || exit /b 1
if "%mule_main%"=="1" set didbuild=1 && del vc*.pdb mule*.pdb && %compile_release% %only_compile% ..\src\mule\mule_inline.cpp && %compile_release% %only_compile% ..\src\mule\mule_o2.cpp && %compile_debug% %EHsc% ..\src\mule\mule_main.cpp ..\src\mule\mule_c.c mule_inline.obj mule_o2.obj %compile_link% %no_aslr% %out%mule_main.exe || exit /b 1
if "%mule_module%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_module.cpp %compile_link% %link_dll% %out%mule_module.dll || exit /b 1
if "%mule_hotload%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_hotload_main.c %compile_link% %out%mule_hotload.exe & %compile% ..\src\mule\mule_hotload_module_main.c %compile_link% %link_dll% %out%mule_hotload_module.dll || exit /b 1
+53 -10
View File
@@ -58,6 +58,10 @@ arena_alloc_(ArenaParams *params)
arena->pos = ARENA_HEADER_SIZE;
arena->cmt = commit_size;
arena->res = reserve_size;
#if ARENA_FREE_LIST
arena->free_size = 0;
arena->free_last = 0;
#endif
AsanPoisonMemoryRegion(base, commit_size);
AsanUnpoisonMemoryRegion(base, ARENA_HEADER_SIZE);
return arena;
@@ -85,18 +89,45 @@ arena_push(Arena *arena, U64 size, U64 align)
// rjf: chain, if needed
if(current->res < pos_pst && !(arena->flags & ArenaFlag_NoChain))
{
U64 res_size = current->res_size;
U64 cmt_size = current->cmt_size;
if(size + ARENA_HEADER_SIZE > res_size)
Arena *new_block = 0;
#if ARENA_FREE_LIST
Arena *prev_block;
for(new_block = arena->free_last, prev_block = 0; new_block != 0; prev_block = new_block, new_block = new_block->prev)
{
res_size = size + ARENA_HEADER_SIZE;
cmt_size = size + ARENA_HEADER_SIZE;
if(new_block->res >= AlignPow2(size, align))
{
if(prev_block)
{
prev_block->prev = new_block->prev;
}
else
{
arena->free_last = new_block->prev;
}
arena->free_size -= new_block->res_size;
AsanUnpoisonMemoryRegion((U8*)new_block + ARENA_HEADER_SIZE, new_block->res_size - ARENA_HEADER_SIZE);
break;
}
}
Arena *new_block = arena_alloc(.reserve_size = res_size,
.commit_size = cmt_size,
.flags = current->flags);
new_block->base_pos = current->base_pos + current->res;
SLLStackPush_N(arena->current, new_block, prev);
#endif
if(new_block == 0)
{
U64 res_size = current->res_size;
U64 cmt_size = current->cmt_size;
if(size + ARENA_HEADER_SIZE > res_size)
{
res_size = AlignPow2(size + ARENA_HEADER_SIZE, align);
cmt_size = AlignPow2(size + ARENA_HEADER_SIZE, align);
}
new_block = arena_alloc(.reserve_size = res_size,
.commit_size = cmt_size,
.flags = current->flags);
new_block->base_pos = current->base_pos + current->res;
SLLStackPush_N(arena->current, new_block, prev);
}
current = new_block;
pos_pre = AlignPow2(current->pos, align);
pos_pst = pos_pre + size;
@@ -155,11 +186,23 @@ arena_pop_to(Arena *arena, U64 pos)
{
U64 big_pos = ClampBot(ARENA_HEADER_SIZE, pos);
Arena *current = arena->current;
#if ARENA_FREE_LIST
for(Arena *prev = 0; current->base_pos >= big_pos; current = prev)
{
prev = current->prev;
current->pos = ARENA_HEADER_SIZE;
arena->free_size += current->res_size;
SLLStackPush_N(arena->free_last, current, prev);
AsanPoisonMemoryRegion((U8*)current + ARENA_HEADER_SIZE, current->res_size - ARENA_HEADER_SIZE);
}
#else
for(Arena *prev = 0; current->base_pos >= big_pos; current = prev)
{
prev = current->prev;
os_release(current, current->res);
}
#endif
arena->current = current;
U64 new_pos = big_pos - current->base_pos;
AssertAlways(new_pos <= current->pos);
+9 -1
View File
@@ -7,7 +7,11 @@
////////////////////////////////
//~ rjf: Constants
#define ARENA_HEADER_SIZE 64
#if ARENA_FREE_LIST
# define ARENA_HEADER_SIZE 128
#else
# define ARENA_HEADER_SIZE 64
#endif
////////////////////////////////
//~ rjf: Types
@@ -40,6 +44,10 @@ struct Arena
U64 pos;
U64 cmt;
U64 res;
#if ARENA_FREE_LIST
U64 free_size;
Arena *free_last;
#endif
};
StaticAssert(sizeof(Arena) <= ARENA_HEADER_SIZE, arena_header_size_check);
+1
View File
@@ -86,6 +86,7 @@ enum
typedef enum PathStyle
{
PathStyle_Null,
PathStyle_Relative,
PathStyle_WindowsAbsolute,
PathStyle_UnixAbsolute,
+16 -6
View File
@@ -80,6 +80,13 @@ cv_numeric_from_data_range(U8 *first, U8 *opl)
return result;
}
internal U64
cv_read_numeric(String8 data, U64 offset, CV_NumericParsed *out)
{
*out = cv_numeric_from_data_range(data.str + offset, data.str + data.size);
return out->encoded_size;
}
internal B32
cv_numeric_fits_in_u64(CV_NumericParsed *num)
{
@@ -186,7 +193,7 @@ cv_decode_inline_annot_u32(String8 data, U64 offset, U32 *out_value)
}
// 2 bytes
else if((header & 0xC0) == 0x80 && cursor+2 <= data.size)
else if((header & 0xC0) == 0x80 && cursor+1 <= data.size)
{
U8 second_byte;
cursor += str8_deserial_read_struct(data, cursor, &second_byte);
@@ -411,7 +418,7 @@ cv_leaf_from_data(Arena *arena, String8 leaf_data, CV_TypeId itype_first)
//~ CodeView C13 Parser Functions
internal CV_C13Parsed *
cv_c13_parsed_from_data(Arena *arena, String8 c13_data, PDB_Strtbl *strtbl, PDB_CoffSectionArray *sections)
cv_c13_parsed_from_data(Arena *arena, String8 c13_data, String8 strtbl, COFF_SectionHeaderArray sections)
{
ProfBeginFunction();
@@ -488,10 +495,10 @@ cv_c13_parsed_from_data(Arena *arena, String8 c13_data, PDB_Strtbl *strtbl, PDB_
B32 has_cols = !!(hdr->flags & CV_C13SubSecLinesFlag_HasColumns);
U64 secrel_off = hdr->sec_off;
U64 secrel_opl = secrel_off + hdr->len;
U64 sec_base_off = sections->sections[sec_idx - 1].voff;
U64 sec_base_off = sections.v[sec_idx - 1].voff;
// rjf: bad section index -> skip
if(sec_idx < 1 || sections->count < sec_idx)
if(sec_idx < 1 || sections.count < sec_idx)
{
continue;
}
@@ -511,7 +518,8 @@ cv_c13_parsed_from_data(Arena *arena, String8 c13_data, PDB_Strtbl *strtbl, PDB_
{
CV_C13Checksum *checksum = (CV_C13Checksum*)(c13_data.str + file_chksms->off + file_off);
U32 name_off = checksum->name_off;
file_name = pdb_strtbl_string_from_off(strtbl, name_off);
file_name = str8_cstring_capped((char*)(strtbl.str + name_off),
(char*)(strtbl.str + strtbl.size));
}
// array layouts
@@ -587,7 +595,8 @@ cv_c13_parsed_from_data(Arena *arena, String8 c13_data, PDB_Strtbl *strtbl, PDB_
{
CV_C13Checksum *checksum = (CV_C13Checksum*)(c13_data.str + file_chksms->off + hdr->file_off);
U32 name_off = checksum->name_off;
file_name = pdb_strtbl_string_from_off(strtbl, name_off);
file_name = str8_cstring_capped((char*)(strtbl.str + name_off),
(char*)(strtbl.str + strtbl.size));
}
// rjf: parse extra files
@@ -608,6 +617,7 @@ cv_c13_parsed_from_data(Arena *arena, String8 c13_data, PDB_Strtbl *strtbl, PDB_
SLLQueuePush(node->inlinee_lines_first, node->inlinee_lines_last, n);
n->v.inlinee = hdr->inlinee;
n->v.file_name = file_name;
n->v.file_off = hdr->file_off;
n->v.first_source_ln = hdr->first_source_ln;
n->v.extra_file_count = extra_file_count;
n->v.extra_files = extra_files;
+138 -34
View File
@@ -9,9 +9,15 @@
////////////////////////////////
//~ rjf: CodeView Format Shared Types
typedef U32 CV_TypeId;
typedef U32 CV_ItemId;
#define CV_TypeIndex_Max max_U32
typedef U32 CV_TypeIndex;
typedef CV_TypeIndex CV_TypeId;
typedef CV_TypeIndex CV_ItemId;
#define CV_ModIndex_Max max_U16
#define CV_ModIndex_Invalid CV_ModIndex_Max
typedef U16 CV_ModIndex;
typedef U16 CV_SectionIndex;
typedef U16 CV_Reg;
@@ -898,7 +904,95 @@ X(YMM14D3, 683, zmm14, 24, 8)\
X(YMM15D0, 684, zmm15, 0, 8)\
X(YMM15D1, 685, zmm15, 8, 8)\
X(YMM15D2, 686, zmm15, 16, 8)\
X(YMM15D3, 687, zmm15, 24, 8)
X(YMM15D3, 687, zmm15, 24, 8)\
X(XMM16, 694, zmm16, 0, 16)\
X(XMM17, 695, zmm17, 0, 16)\
X(XMM18, 696, zmm18, 0, 16)\
X(XMM19, 697, zmm19, 0, 16)\
X(XMM20, 698, zmm20, 0, 16)\
X(XMM21, 699, zmm21, 0, 16)\
X(XMM22, 700, zmm22, 0, 16)\
X(XMM23, 701, zmm23, 0, 16)\
X(XMM24, 702, zmm24, 0, 16)\
X(XMM25, 703, zmm25, 0, 16)\
X(XMM26, 704, zmm26, 0, 16)\
X(XMM27, 705, zmm27, 0, 16)\
X(XMM28, 706, zmm28, 0, 16)\
X(XMM29, 707, zmm29, 0, 16)\
X(XMM30, 708, zmm30, 0, 16)\
X(XMM31, 709, zmm31, 0, 16)\
X(YMM16, 710, zmm16, 0, 32)\
X(YMM17, 711, zmm17, 0, 32)\
X(YMM18, 712, zmm18, 0, 32)\
X(YMM19, 713, zmm19, 0, 32)\
X(YMM20, 714, zmm20, 0, 32)\
X(YMM21, 715, zmm21, 0, 32)\
X(YMM22, 716, zmm22, 0, 32)\
X(YMM23, 717, zmm23, 0, 32)\
X(YMM24, 718, zmm24, 0, 32)\
X(YMM25, 719, zmm25, 0, 32)\
X(YMM26, 720, zmm26, 0, 32)\
X(YMM27, 721, zmm27, 0, 32)\
X(YMM28, 722, zmm28, 0, 32)\
X(YMM29, 723, zmm29, 0, 32)\
X(YMM30, 724, zmm30, 0, 32)\
X(YMM31, 725, zmm31, 0, 32)\
X(ZMM0, 726, zmm0, 0, 64)\
X(ZMM1, 727, zmm1, 0, 64)\
X(ZMM2, 728, zmm2, 0, 64)\
X(ZMM3, 729, zmm3, 0, 64)\
X(ZMM4, 730, zmm4, 0, 64)\
X(ZMM5, 731, zmm5, 0, 64)\
X(ZMM6, 732, zmm6, 0, 64)\
X(ZMM7, 733, zmm7, 0, 64)\
X(ZMM8, 734, zmm8, 0, 64)\
X(ZMM9, 735, zmm9, 0, 64)\
X(ZMM10, 736, zmm10, 0, 64)\
X(ZMM11, 737, zmm11, 0, 64)\
X(ZMM12, 738, zmm12, 0, 64)\
X(ZMM13, 739, zmm13, 0, 64)\
X(ZMM14, 740, zmm14, 0, 64)\
X(ZMM15, 741, zmm15, 0, 64)\
X(ZMM16, 742, zmm16, 0, 64)\
X(ZMM17, 743, zmm17, 0, 64)\
X(ZMM18, 744, zmm18, 0, 64)\
X(ZMM19, 745, zmm19, 0, 64)\
X(ZMM20, 746, zmm20, 0, 64)\
X(ZMM21, 747, zmm21, 0, 64)\
X(ZMM22, 748, zmm22, 0, 64)\
X(ZMM23, 749, zmm23, 0, 64)\
X(ZMM24, 750, zmm24, 0, 64)\
X(ZMM25, 751, zmm25, 0, 64)\
X(ZMM26, 752, zmm26, 0, 64)\
X(ZMM27, 753, zmm27, 0, 64)\
X(ZMM28, 754, zmm28, 0, 64)\
X(ZMM29, 755, zmm29, 0, 64)\
X(ZMM30, 756, zmm30, 0, 64)\
X(ZMM31, 757, zmm31, 0, 64)\
X(K0, 758, k0, 0, 8)\
X(K1, 759, k1, 0, 8)\
X(K2, 760, k2, 0, 8)\
X(K3, 761, k3, 0, 8)\
X(K4, 762, k4, 0, 8)\
X(K5, 763, k5, 0, 8)\
X(K6, 764, k6, 0, 8)\
X(K7, 765, k7, 0, 8)\
X(ZMM0H, 766, zmm0, 32, 32)\
X(ZMM1H, 767, zmm1, 32, 32)\
X(ZMM2H, 768, zmm2, 32, 32)\
X(ZMM3H, 769, zmm3, 32, 32)\
X(ZMM4H, 770, zmm4, 32, 32)\
X(ZMM5H, 771, zmm5, 32, 32)\
X(ZMM6H, 772, zmm6, 32, 32)\
X(ZMM7H, 773, zmm7, 32, 32)\
X(ZMM8H, 774, zmm8, 32, 32)\
X(ZMM9H, 775, zmm9, 32, 32)\
X(ZMM10H, 776, zmm10, 32, 32)\
X(ZMM11H, 777, zmm11, 32, 32)\
X(ZMM12H, 778, zmm12, 32, 32)\
X(ZMM13H, 779, zmm13, 32, 32)\
X(ZMM14H, 780, zmm14, 32, 32)\
X(ZMM15H, 781, zmm15, 32, 32)
typedef U16 CV_Regx64;
typedef enum CV_Regx64Enum
@@ -917,7 +1011,7 @@ X(C11, 2)\
X(C13, 4)\
X(RESERVED, 5)
typedef U16 CV_Signature;
typedef U32 CV_Signature;
typedef enum CV_SignatureEnum
{
#define X(N,c) CV_Signature_##N = c,
@@ -1164,8 +1258,8 @@ struct CV_SymAnnotation
//- (SymKind: OBJNAME)
typedef struct CV_SymObjname CV_SymObjname;
struct CV_SymObjname
typedef struct CV_SymObjName CV_SymObjName;
struct CV_SymObjName
{
U32 sig;
// U8[] name (null terminated)
@@ -1286,19 +1380,19 @@ struct CV_SymData32
//- (SymKind: PUB32)
typedef U32 CV_PubFlags;
typedef U32 CV_Pub32Flags;
enum
{
CV_PubFlag_Code = (1 << 0),
CV_PubFlag_Function = (1 << 1),
CV_PubFlag_ManagedCode = (1 << 2),
CV_PubFlag_MSIL = (1 << 3),
CV_Pub32Flag_Code = (1 << 0),
CV_Pub32Flag_Function = (1 << 1),
CV_Pub32Flag_ManagedCode = (1 << 2),
CV_Pub32Flag_MSIL = (1 << 3),
};
typedef struct CV_SymPub32 CV_SymPub32;
struct CV_SymPub32
{
CV_PubFlags flags;
CV_Pub32Flags flags;
U32 off;
CV_SectionIndex sec;
// U8[] name (null terminated)
@@ -1607,6 +1701,24 @@ struct CV_SymDiscarded
};
//- (SymKind: COMPILE3)
typedef U32 CV_Compile3Flags;
enum
{
CV_Compile3Flag_EC = (1 << 8),
CV_Compile3Flag_NoDbgInfo = (1 << 9),
CV_Compile3Flag_LTCG = (1 << 10),
CV_Compile3Flag_NoDataAlign = (1 << 11),
CV_Compile3Flag_ManagedPresent = (1 << 12),
CV_Compile3Flag_SecurityChecks = (1 << 13),
CV_Compile3Flag_HotPatch = (1 << 14),
CV_Compile3Flag_CVTCIL = (1 << 15),
CV_Compile3Flag_MSILModule = (1 << 16),
CV_Compile3Flag_SDL = (1 << 17),
CV_Compile3Flag_PGO = (1 << 18),
CV_Compile3Flag_EXP = (1 << 19),
CV_Compile3Flag_Language_Shift = 0, CV_Compile3Flag_Language_Mask = 0xff,
};
typedef U32 CV_Compile3Flags;
#define CV_Compile3Flags_ExtractLanguage(f) (((f) )&0xFF)
@@ -1715,6 +1827,8 @@ struct CV_SymDefrangeFramepointerRel
//- (SymKind: DEFRANGE_SUBFIELD_REGISTER)
#define CV_DefrangeSubfieldRegister_ExtractParentOffset(x) ((x) & 0x1FFF)
typedef struct CV_SymDefrangeSubfieldRegister CV_SymDefrangeSubfieldRegister;
struct CV_SymDefrangeSubfieldRegister
{
@@ -1968,24 +2082,6 @@ struct CV_SymInlinees
// (type info)
//
#define CV_LeafIDKindXList(X) \
X(FUNC_ID, 0x1601)\
X(MFUNC_ID, 0x1602)\
X(BUILDINFO, 0x1603)\
X(SUBSTR_LIST, 0x1604)\
X(STRING_ID, 0x1605)\
X(UDT_SRC_LINE, 0x1606)\
X(UDT_MOD_SRC_LINE, 0x1607)
typedef U16 CV_LeafIDKind;
typedef enum CV_LeafIDKindEnum
{
#define X(N,c) CV_LeafIDKind_##N = c,
CV_LeafIDKindXList(X)
#undef X
}
CV_LeafIDKindEnum;
#define CV_TypeId_Variadic 0
#define CV_BasicPointerKindXList(X) \
@@ -2197,10 +2293,18 @@ struct CV_LeafPreComp
{
U32 start_index;
U32 count;
U32 signature;
U32 sig;
// U8[] name (null terminated)
};
//- (LeafKind; END_PRECOMP)
typedef struct CV_LeafEndPreComp CV_LeafEndPreComp;
struct CV_LeafEndPreComp
{
U32 sig;
};
//- (LeafKind: TYPESERVER)
typedef struct CV_LeafTypeServer CV_LeafTypeServer;
@@ -2916,6 +3020,7 @@ typedef struct CV_C13InlineeLinesParsed CV_C13InlineeLinesParsed;
struct CV_C13InlineeLinesParsed
{
CV_ItemId inlinee;
U32 file_off;
String8 file_name;
U32 first_source_ln;
U32 extra_file_count;
@@ -2979,6 +3084,7 @@ internal U64 cv_hash_from_string(String8 string);
internal U64 cv_hash_from_item_id(CV_ItemId item_id);
internal CV_NumericParsed cv_numeric_from_data_range(U8 *first, U8 *opl);
internal U64 cv_read_numeric(String8 data, U64 offset, CV_NumericParsed *out);
internal B32 cv_numeric_fits_in_u64(CV_NumericParsed *num);
internal B32 cv_numeric_fits_in_s64(CV_NumericParsed *num);
@@ -3010,8 +3116,6 @@ internal CV_LeafParsed *cv_leaf_from_data(Arena *arena, String8 leaf_data, CV_Ty
////////////////////////////////
//~ CodeView C13 Parser Functions
typedef struct PDB_Strtbl PDB_Strtbl;
typedef struct PDB_CoffSectionArray PDB_CoffSectionArray;
internal CV_C13Parsed *cv_c13_parsed_from_data(Arena *arena, String8 c13_data, struct PDB_Strtbl *strtbl, struct PDB_CoffSectionArray *sections);
internal CV_C13Parsed *cv_c13_parsed_from_data(Arena *arena, String8 c13_data, String8 strtbl, COFF_SectionHeaderArray sections);
#endif // CODEVIEW_H
+9 -1
View File
@@ -246,7 +246,7 @@ cv_string_from_arch:
{MANTYPREF - 0x1028}
{UNAMESPACE_ST - 0x1029}
{ST_MAX - 0x1100}
{OBJNAME Objname 0x1101}
{OBJNAME ObjName 0x1101}
{THUNK32 Thunk32 0x1102}
{BLOCK32 Block32 0x1103}
{WITH32 - 0x1104}
@@ -462,6 +462,7 @@ CV_BasicTypeTable:
@table(name header_type_name val)
CV_LeafKindTable:
{
{NOTYPE - 0x0000}
{MODIFIER_16t - 0x0001}
{POINTER_16t - 0x0002}
{ARRAY_16t - 0x0003}
@@ -586,6 +587,13 @@ CV_LeafKindTable:
{VECTOR - 0x151b}
{MATRIX - 0x151c}
{VFTABLE - 0x151d}
{FUNC_ID FuncId 0x1601}
{MFUNC_ID MFuncId 0x1602}
{BUILDINFO BuildInfo 0x1603}
{SUBSTR_LIST SubstrList 0x1604}
{STRING_ID StringId 0x1605}
{UDT_SRC_LINE UDTSrcLine 0x1606}
{UDT_MOD_SRC_LINE UDTModSrcLine 0x1607}
{CLASS2 Struct2 0x1608}
{STRUCT2 Struct2 0x1609}
}
+26 -14
View File
@@ -408,11 +408,11 @@ cv_stringize_sym_range(Arena *arena, String8List *out,
case CV_SymKind_OBJNAME:
{
if (sizeof(CV_SymObjname) > cap){
if (sizeof(CV_SymObjName) > cap){
str8_list_push(arena, out, str8_lit(" bad symbol range\n"));
}
else{
CV_SymObjname *objname = (CV_SymObjname*)first;
CV_SymObjName *objname = (CV_SymObjName*)first;
// sig
str8_list_pushf(arena, out, " sig=%u\n", objname->sig);
@@ -568,22 +568,22 @@ cv_stringize_sym_range(Arena *arena, String8List *out,
CV_SymPub32 *pub32 = (CV_SymPub32*)first;
// flags
CV_PubFlags flags = pub32->flags;
CV_Pub32Flags flags = pub32->flags;
str8_list_push(arena, out, str8_lit(" flags="));
if (flags == 0){
str8_list_push(arena, out, str8_lit("0|"));
}
else{
if (flags&CV_PubFlag_Code){
if (flags&CV_Pub32Flag_Code){
str8_list_push(arena, out, str8_lit("Code|"));
}
if (flags&CV_PubFlag_Function){
if (flags&CV_Pub32Flag_Function){
str8_list_push(arena, out, str8_lit("Function|"));
}
if (flags&CV_PubFlag_ManagedCode){
if (flags&CV_Pub32Flag_ManagedCode){
str8_list_push(arena, out, str8_lit("ManagedCode|"));
}
if (flags&CV_PubFlag_MSIL){
if (flags&CV_Pub32Flag_MSIL){
str8_list_push(arena, out, str8_lit("MSIL|"));
}
}
@@ -2004,7 +2004,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_FUNC_ID:
case CV_LeafKind_FUNC_ID:
{
if (sizeof(CV_LeafFuncId) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2020,7 +2020,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_MFUNC_ID:
case CV_LeafKind_MFUNC_ID:
{
if (sizeof(CV_LeafMFuncId) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2036,7 +2036,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_BUILDINFO:
case CV_LeafKind_BUILDINFO:
{
if (sizeof(CV_LeafBuildInfo) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2057,7 +2057,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_SUBSTR_LIST:
case CV_LeafKind_SUBSTR_LIST:
{
if (sizeof(CV_LeafSubstrList) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2078,7 +2078,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_STRING_ID:
case CV_LeafKind_STRING_ID:
{
if (sizeof(CV_LeafStringId) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2093,7 +2093,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
}
}break;
case CV_LeafIDKind_UDT_SRC_LINE:
case CV_LeafKind_UDT_SRC_LINE:
{
if (sizeof(CV_LeafUDTSrcLine) > cap){
str8_list_push(arena, out, str8_lit(" bad leaf range\n"));
@@ -2243,7 +2243,7 @@ cv_stringize_leaf_range(Arena *arena, String8List *out,
//case CV_LeafIDKind_SUBSTR_LIST:
//case CV_LeafIDKind_STRING_ID:
//case CV_LeafIDKind_UDT_SRC_LINE:
case CV_LeafIDKind_UDT_MOD_SRC_LINE:
case CV_LeafKind_UDT_MOD_SRC_LINE:
{
str8_list_push(arena, out, str8_lit(" no stringizer path\n"));
@@ -2328,3 +2328,15 @@ cv_stringize_c13_parsed(Arena *arena, String8List *out, CV_C13Parsed *c13){
str8_list_push(arena, out, str8_lit("\n"));
}
}
internal String8
cv_string_from_inline_range_kind(CV_InlineRangeKind kind)
{
String8 result = str8_zero();
switch (kind) {
case CV_InlineRangeKind_Expr: result = str8_lit("Expr"); break;
case CV_InlineRangeKind_Stmt: result = str8_lit("Stmt"); break;
}
return result;
}
+2
View File
@@ -79,4 +79,6 @@ internal void cv_stringize_leaf_array(Arena *arena, String8List *out,
internal void cv_stringize_c13_parsed(Arena *arena, String8List *out, CV_C13Parsed *c13);
internal String8 cv_string_from_inline_range_kind(CV_InlineRangeKind kind);
#endif // CODEVIEW_STRINGIZE_H
+16 -1
View File
@@ -447,6 +447,7 @@ String8 result = str8_lit("<Unknown CV_LeafKind>");
switch(v)
{
default:{}break;
case CV_LeafKind_NOTYPE:{result = str8_lit("NOTYPE");}break;
case CV_LeafKind_MODIFIER_16t:{result = str8_lit("MODIFIER_16t");}break;
case CV_LeafKind_POINTER_16t:{result = str8_lit("POINTER_16t");}break;
case CV_LeafKind_ARRAY_16t:{result = str8_lit("ARRAY_16t");}break;
@@ -571,6 +572,13 @@ case CV_LeafKind_BINTERFACE:{result = str8_lit("BINTERFACE");}break;
case CV_LeafKind_VECTOR:{result = str8_lit("VECTOR");}break;
case CV_LeafKind_MATRIX:{result = str8_lit("MATRIX");}break;
case CV_LeafKind_VFTABLE:{result = str8_lit("VFTABLE");}break;
case CV_LeafKind_FUNC_ID:{result = str8_lit("FUNC_ID");}break;
case CV_LeafKind_MFUNC_ID:{result = str8_lit("MFUNC_ID");}break;
case CV_LeafKind_BUILDINFO:{result = str8_lit("BUILDINFO");}break;
case CV_LeafKind_SUBSTR_LIST:{result = str8_lit("SUBSTR_LIST");}break;
case CV_LeafKind_STRING_ID:{result = str8_lit("STRING_ID");}break;
case CV_LeafKind_UDT_SRC_LINE:{result = str8_lit("UDT_SRC_LINE");}break;
case CV_LeafKind_UDT_MOD_SRC_LINE:{result = str8_lit("UDT_MOD_SRC_LINE");}break;
case CV_LeafKind_CLASS2:{result = str8_lit("CLASS2");}break;
case CV_LeafKind_STRUCT2:{result = str8_lit("STRUCT2");}break;
}
@@ -592,7 +600,7 @@ case CV_SymKind_OEM:{result = sizeof(CV_SymOEM);}break;
case CV_SymKind_VFTABLE32:{result = sizeof(CV_SymVPath32);}break;
case CV_SymKind_FRAMEPROC:{result = sizeof(CV_SymFrameproc);}break;
case CV_SymKind_ANNOTATION:{result = sizeof(CV_SymAnnotation);}break;
case CV_SymKind_OBJNAME:{result = sizeof(CV_SymObjname);}break;
case CV_SymKind_OBJNAME:{result = sizeof(CV_SymObjName);}break;
case CV_SymKind_THUNK32:{result = sizeof(CV_SymThunk32);}break;
case CV_SymKind_BLOCK32:{result = sizeof(CV_SymBlock32);}break;
case CV_SymKind_LABEL32:{result = sizeof(CV_SymLabel32);}break;
@@ -696,6 +704,13 @@ case CV_LeafKind_ONEMETHOD:{result = sizeof(CV_LeafOneMethod);}break;
case CV_LeafKind_NESTTYPEEX:{result = sizeof(CV_LeafNestTypeEx);}break;
case CV_LeafKind_TYPESERVER2:{result = sizeof(CV_LeafTypeServer2);}break;
case CV_LeafKind_INTERFACE:{result = sizeof(CV_LeafStruct);}break;
case CV_LeafKind_FUNC_ID:{result = sizeof(CV_LeafFuncId);}break;
case CV_LeafKind_MFUNC_ID:{result = sizeof(CV_LeafMFuncId);}break;
case CV_LeafKind_BUILDINFO:{result = sizeof(CV_LeafBuildInfo);}break;
case CV_LeafKind_SUBSTR_LIST:{result = sizeof(CV_LeafSubstrList);}break;
case CV_LeafKind_STRING_ID:{result = sizeof(CV_LeafStringId);}break;
case CV_LeafKind_UDT_SRC_LINE:{result = sizeof(CV_LeafUDTSrcLine);}break;
case CV_LeafKind_UDT_MOD_SRC_LINE:{result = sizeof(CV_LeafUDTModSrcLine);}break;
case CV_LeafKind_CLASS2:{result = sizeof(CV_LeafStruct2);}break;
case CV_LeafKind_STRUCT2:{result = sizeof(CV_LeafStruct2);}break;
}
+8
View File
@@ -381,6 +381,7 @@ CV_BasicType_PTR = 0xf0,
typedef U16 CV_LeafKind;
typedef enum CV_LeafKindEnum
{
CV_LeafKind_NOTYPE = 0x0000,
CV_LeafKind_MODIFIER_16t = 0x0001,
CV_LeafKind_POINTER_16t = 0x0002,
CV_LeafKind_ARRAY_16t = 0x0003,
@@ -505,6 +506,13 @@ CV_LeafKind_BINTERFACE = 0x151a,
CV_LeafKind_VECTOR = 0x151b,
CV_LeafKind_MATRIX = 0x151c,
CV_LeafKind_VFTABLE = 0x151d,
CV_LeafKind_FUNC_ID = 0x1601,
CV_LeafKind_MFUNC_ID = 0x1602,
CV_LeafKind_BUILDINFO = 0x1603,
CV_LeafKind_SUBSTR_LIST = 0x1604,
CV_LeafKind_STRING_ID = 0x1605,
CV_LeafKind_UDT_SRC_LINE = 0x1606,
CV_LeafKind_UDT_MOD_SRC_LINE = 0x1607,
CV_LeafKind_CLASS2 = 0x1608,
CV_LeafKind_STRUCT2 = 0x1609,
} CV_LeafKindEnum;
+312 -54
View File
@@ -387,12 +387,34 @@ coff_word_size_from_machine(COFF_MachineType machine)
{
U64 result = 0;
switch (machine) {
case COFF_MachineType_X64: result = 8; break;
case COFF_MachineType_X86: result = 4; break;
case COFF_MachineType_X64: result = 8; break;
case COFF_MachineType_X86: result = 4; break;
}
return result;
}
internal U64
coff_default_exe_base_from_machine(COFF_MachineType machine)
{
U64 exe_base = 0;
switch (coff_word_size_from_machine(machine)) {
case 4: exe_base = 0x400000; break;
case 8: exe_base = 0x140000000; break;
}
return exe_base;
}
internal U64
coff_default_dll_base_from_machine(COFF_MachineType machine)
{
U64 dll_base = 0;
switch (coff_word_size_from_machine(machine)) {
case 4: dll_base = 0x10000000; break;
case 8: dll_base = 0x180000000; break;
}
return dll_base;
}
internal String8
coff_make_import_lookup(Arena *arena, U16 hint, String8 name)
{
@@ -421,6 +443,114 @@ coff_make_ordinal_64(U16 hint)
////////////////////////////////
internal String8
coff_make_import_header_by_name(Arena *arena,
String8 dll_name,
COFF_MachineType machine,
COFF_TimeStamp time_stamp,
String8 name,
U16 hint,
COFF_ImportHeaderType type)
{
struct {
U16 sig1;
U16 sig2;
U16 version;
COFF_MachineType machine;
COFF_TimeStamp time_stamp;
U32 sizeof_data;
U16 hint_ordinal;
U16 flags;
} import_header = {
COFF_MachineType_UNKNOWN, // sig1
max_U16, // sig2
0, // version
machine,
time_stamp,
safe_cast_u32(name.size + dll_name.size + 2), // sizeof_data
0, // hint_ordinal
0, // flags
};
import_header.flags |= (U16)(type & COFF_IMPORT_HEADER_TYPE_MASK) << COFF_IMPORT_HEADER_TYPE_SHIFT;
import_header.flags |= COFF_ImportHeaderNameType_NAME << COFF_IMPORT_HEADER_NAME_TYPE_SHIFT;
import_header.hint_ordinal = hint;
// alloc memory
U64 buffer_size = sizeof(import_header) + import_header.sizeof_data;
U8 *buffer = push_array_no_zero(arena, U8, buffer_size);
// copy header
MemoryCopy(buffer, &import_header, sizeof(import_header));
// copy function name
U8 *func_name = buffer + sizeof(import_header);
MemoryCopy(func_name, name.str, name.size);
func_name[name.size] = 0;
// copy dll name
U8 *dll_name_buffer = buffer + sizeof(import_header) + name.size + 1;
MemoryCopy(dll_name_buffer, dll_name.str, dll_name.size);
dll_name_buffer[dll_name.size] = 0;
String8 import_data = str8(buffer, buffer_size);
return import_data;
}
internal String8
coff_make_import_header_by_ordinal(Arena *arena,
String8 dll_name,
COFF_MachineType machine,
COFF_TimeStamp time_stamp,
U16 ordinal,
COFF_ImportHeaderType type)
{
struct {
U16 sig1;
U16 sig2;
U16 version;
COFF_MachineType machine;
COFF_TimeStamp time_stamp;
U32 sizeof_data;
U16 hint_ordinal;
U16 flags;
} import_header = {
COFF_MachineType_UNKNOWN, // sig1
max_U16, // sig2
0, // version
machine,
time_stamp,
safe_cast_u32(/* name.size + */ dll_name.size + 2), // sizeof_data
0, // hint_ordinal
0, // flags
};
import_header.flags |= (U16)(type & COFF_IMPORT_HEADER_TYPE_MASK) << COFF_IMPORT_HEADER_TYPE_SHIFT;
import_header.flags |= COFF_ImportHeaderNameType_ORDINAL << COFF_IMPORT_HEADER_NAME_TYPE_SHIFT;
import_header.hint_ordinal = ordinal;
// alloc memory
U64 buffer_size = sizeof(import_header) + import_header.sizeof_data;
U8 *buffer = push_array_no_zero(arena, U8, buffer_size);
// copy header
MemoryCopyStruct(buffer, &import_header);
// no function name write zero
U8 *func_name = buffer + sizeof(import_header);
func_name[0] = 0;
// copy dll name
U8 *dll_name_buffer = buffer + sizeof(import_header) + /* name.size */ + 1;
MemoryCopy(dll_name_buffer, dll_name.str, dll_name.size);
dll_name_buffer[dll_name.size] = 0;
String8 import_data = str8(buffer, buffer_size);
return import_data;
}
////////////////////////////////
internal B32
coff_resource_id_is_equal(COFF_ResourceID a, COFF_ResourceID b)
{
@@ -455,6 +585,25 @@ coff_resource_id_copy(Arena *arena, COFF_ResourceID id)
return result;
}
internal U64
coff_sizeof_resource_id(COFF_ResourceID id)
{
U64 size = 0;
switch (id.type) {
case COFF_ResourceIDType_NULL: break;
case COFF_ResourceIDType_NUMBER: {
size += sizeof(U16);
size += sizeof(U16);
} break;
case COFF_ResourceIDType_STRING: {
size += sizeof(U16);
size += (id.u.string.size + /* null: */1) * sizeof(U16);
} break;
default: InvalidPath;
}
return AlignPow2(size, COFF_RES_ALIGN);
}
internal COFF_ResourceID
coff_convert_resource_id(Arena *arena, COFF_ResourceID_16 *id_16)
{
@@ -558,6 +707,67 @@ coff_resource_list_from_data(Arena *arena, String8 data)
return list;
}
internal void
coff_write_resource_id(Arena *arena, String8List *srl, COFF_ResourceID id)
{
switch (id.type) {
case COFF_ResourceIDType_NULL: break;
case COFF_ResourceIDType_NUMBER: {
str8_serial_push_u16(arena, srl, max_U16);
str8_serial_push_u16(arena, srl, id.u.number);
} break;
case COFF_ResourceIDType_STRING: {
String16 str16 = str16_from_8(arena, id.u.string);
Assert(str16.size <= max_U16);
str8_serial_push_u16(arena, srl, str16.size);
str8_serial_push_data(arena, srl, str16.str, str16.size);
} break;
default: InvalidPath;
}
str8_serial_push_align(arena, srl, COFF_RES_ALIGN);
}
internal String8List
coff_write_resource(Arena *arena,
COFF_ResourceID type,
COFF_ResourceID name,
U32 data_version,
COFF_ResourceMemoryFlags memory_flags,
U16 language_id,
U32 version,
U32 characteristics,
String8 data)
{
U64 header_size = sizeof(COFF_ResourceHeaderPrefix) +
coff_sizeof_resource_id(type) +
coff_sizeof_resource_id(name) +
sizeof(data_version) +
sizeof(memory_flags) +
sizeof(language_id) +
sizeof(version) +
sizeof(characteristics);
COFF_ResourceHeaderPrefix *header = push_array(arena, COFF_ResourceHeaderPrefix, 1);
header->data_size = safe_cast_u32(data.size);
header->header_size = safe_cast_u32(header_size);
String8List srl = {0};
str8_serial_begin(arena, &srl);
str8_serial_push_data(arena, &srl, &g_coff_res_magic[0], sizeof(g_coff_res_magic));
str8_serial_push_data(arena, &srl, header, sizeof(*header));
coff_write_resource_id(arena, &srl, type);
coff_write_resource_id(arena, &srl, name);
str8_serial_push_u32(arena, &srl, data_version);
str8_serial_push_u16(arena, &srl, memory_flags);
str8_serial_push_u16(arena, &srl, language_id);
str8_serial_push_u32(arena, &srl, version);
str8_serial_push_u32(arena, &srl, characteristics);
str8_serial_push_string(arena, &srl, data);
str8_serial_push_align(arena, &srl, COFF_RES_ALIGN);
return srl;
}
////////////////////////////////
internal COFF_DataType
@@ -591,18 +801,22 @@ coff_is_import(String8 data)
internal B32
coff_is_archive(String8 data)
{
U64 sig = 0;
str8_deserial_read_struct(data, 0, &sig);
B32 is_archive = sig == COFF_ARCHIVE_SIG;
B32 is_archive = 0;
U8 sig[sizeof(g_coff_archive_sig)];
if (str8_deserial_read_struct(data, 0, &sig) == sizeof(sig)) {
is_archive = MemoryCompare(&sig[0], &g_coff_archive_sig[0], sizeof(g_coff_archive_sig)) == 0;
}
return is_archive;
}
internal B32
coff_is_thin_archive(String8 data)
{
U64 sig = 0;
str8_deserial_read_struct(data, 0, &sig);
B32 is_archive = sig == COFF_THIN_ARCHIVE_SIG;
B32 is_archive = 0;
U8 sig[sizeof(g_coff_thin_archive_sig)];
if (str8_deserial_read_struct(data, 0, &sig) == sizeof(sig)) {
is_archive = MemoryCompare(&sig[0], &g_coff_thin_archive_sig[0], sizeof(g_coff_thin_archive_sig)) == 0;
}
return is_archive;
}
@@ -751,11 +965,9 @@ coff_read_archive_long_name(String8 long_names, String8 name)
internal U64
coff_archive_member_iter_init(String8 data)
{
U64 cursor = 0;
U64 sig = 0;
cursor += str8_deserial_read_struct(data, cursor, &sig);
if (sig != COFF_ARCHIVE_SIG) {
cursor = data.size;
U64 cursor = data.size;
if (coff_is_archive(data)) {
cursor = sizeof(g_coff_archive_sig);
}
return cursor;
}
@@ -915,11 +1127,9 @@ coff_archive_from_data(Arena *arena, String8 data)
internal U64
coff_thin_archive_member_iter_init(String8 data)
{
U64 cursor = 0;
U64 sig = 0;
cursor += str8_deserial_read_struct(data, cursor, &sig);
if (sig != COFF_THIN_ARCHIVE_SIG) {
cursor = data.size;
U64 cursor = data.size;
if (coff_is_thin_archive(data)) {
cursor = sizeof(g_coff_thin_archive_sig);
}
return cursor;
}
@@ -990,18 +1200,58 @@ coff_archive_parse_from_data(Arena *arena, String8 data)
////////////////////////////////
read_only struct
{
String8 string;
COFF_MachineType machine;
} g_coff_machine_map[] = {
{ str8_lit_comp(""), COFF_MachineType_UNKNOWN },
{ str8_lit_comp("X86"), COFF_MachineType_X86 },
{ str8_lit_comp("X64"), COFF_MachineType_X64 },
{ str8_lit_comp("AM33"), COFF_MachineType_AM33 },
{ str8_lit_comp("ARM"), COFF_MachineType_ARM },
{ str8_lit_comp("ARM64"), COFF_MachineType_ARM64 },
{ str8_lit_comp("ARMNT"), COFF_MachineType_ARMNT },
{ str8_lit_comp("EBC"), COFF_MachineType_EBC },
{ str8_lit_comp("IA64"), COFF_MachineType_IA64 },
{ str8_lit_comp("M32R"), COFF_MachineType_M32R },
{ str8_lit_comp("MIPS16"), COFF_MachineType_MIPS16 },
{ str8_lit_comp("MIPSFPU"), COFF_MachineType_MIPSFPU },
{ str8_lit_comp("MIPSFPU16"), COFF_MachineType_MIPSFPU16 },
{ str8_lit_comp("POWERPC"), COFF_MachineType_POWERPC },
{ str8_lit_comp("POWERPCFP"), COFF_MachineType_POWERPCFP },
{ str8_lit_comp("R4000"), COFF_MachineType_R4000 },
{ str8_lit_comp("RISCV32"), COFF_MachineType_RISCV32 },
{ str8_lit_comp("RISCV64"), COFF_MachineType_RISCV64 },
{ str8_lit_comp("SH3"), COFF_MachineType_SH3 },
{ str8_lit_comp("SH3DSP"), COFF_MachineType_SH3DSP },
{ str8_lit_comp("SH4"), COFF_MachineType_SH4 },
{ str8_lit_comp("SH5"), COFF_MachineType_SH5 },
{ str8_lit_comp("THUMB"), COFF_MachineType_THUMB },
{ str8_lit_comp("WCEMIPSV2"), COFF_MachineType_WCEMIPSV2 },
};
read_only static struct {
char * name;
COFF_ImportHeaderType type;
} g_coff_import_header_type_map[] = {
{ "CODE", COFF_ImportHeaderType_CODE },
{ "DATA", COFF_ImportHeaderType_DATA },
{ "CONST", COFF_ImportHeaderType_CONST },
};
internal String8
coff_string_from_comdat_select_type(COFF_ComdatSelectType select)
{
String8 result = str8(0,0);
switch (select) {
case COFF_ComdatSelectType_NULL: result = str8_lit("NULL"); break;
case COFF_ComdatSelectType_NODUPLICATES: result = str8_lit("NODUPLICATES"); break;
case COFF_ComdatSelectType_ANY: result = str8_lit("ANY"); break;
case COFF_ComdatSelectType_SAME_SIZE: result = str8_lit("SAME_SIZE"); break;
case COFF_ComdatSelectType_EXACT_MATCH: result = str8_lit("EXACT_MATCH"); break;
case COFF_ComdatSelectType_ASSOCIATIVE: result = str8_lit("ASSOCIATIVE"); break;
case COFF_ComdatSelectType_LARGEST: result = str8_lit("LARGEST"); break;
case COFF_ComdatSelectType_NULL: result = str8_lit("NULL"); break;
case COFF_ComdatSelectType_NODUPLICATES: result = str8_lit("NODUPLICATES"); break;
case COFF_ComdatSelectType_ANY: result = str8_lit("ANY"); break;
case COFF_ComdatSelectType_SAME_SIZE: result = str8_lit("SAME_SIZE"); break;
case COFF_ComdatSelectType_EXACT_MATCH: result = str8_lit("EXACT_MATCH"); break;
case COFF_ComdatSelectType_ASSOCIATIVE: result = str8_lit("ASSOCIATIVE"); break;
case COFF_ComdatSelectType_LARGEST: result = str8_lit("LARGEST"); break;
}
return result;
}
@@ -1009,35 +1259,10 @@ coff_string_from_comdat_select_type(COFF_ComdatSelectType select)
internal String8
coff_string_from_machine_type(COFF_MachineType machine)
{
String8 result = str8(0,0);
switch (machine) {
case COFF_MachineType_UNKNOWN: result = str8_lit("UNKNOWN"); break;
case COFF_MachineType_X86: result = str8_lit("X86"); break;
case COFF_MachineType_X64: result = str8_lit("X64"); break;
case COFF_MachineType_AM33: result = str8_lit("AM33"); break;
case COFF_MachineType_ARM: result = str8_lit("ARM"); break;
case COFF_MachineType_ARM64: result = str8_lit("ARM64"); break;
case COFF_MachineType_ARMNT: result = str8_lit("ARMNT"); break;
case COFF_MachineType_EBC: result = str8_lit("EBC"); break;
case COFF_MachineType_IA64: result = str8_lit("IA64"); break;
case COFF_MachineType_M32R: result = str8_lit("M32R"); break;
case COFF_MachineType_MIPS16: result = str8_lit("MIPS16"); break;
case COFF_MachineType_MIPSFPU: result = str8_lit("MIPSFPU"); break;
case COFF_MachineType_MIPSFPU16: result = str8_lit("MIPSFPU16"); break;
case COFF_MachineType_POWERPC: result = str8_lit("POWERPC"); break;
case COFF_MachineType_POWERPCFP: result = str8_lit("POWERPCFP"); break;
case COFF_MachineType_R4000: result = str8_lit("R4000"); break;
case COFF_MachineType_RISCV32: result = str8_lit("RISCV32"); break;
case COFF_MachineType_RISCV64: result = str8_lit("RISCV64"); break;
case COFF_MachineType_RISCV128: result = str8_lit("RISCV128"); break;
case COFF_MachineType_SH3: result = str8_lit("SH3"); break;
case COFF_MachineType_SH3DSP: result = str8_lit("SH3DSP"); break;
case COFF_MachineType_SH4: result = str8_lit("SH4"); break;
case COFF_MachineType_SH5: result = str8_lit("SH5"); break;
case COFF_MachineType_THUMB: result = str8_lit("THUMB"); break;
case COFF_MachineType_WCEMIPSV2: result = str8_lit("WCEMIPSV2"); break;
}
return result;
Assert(machine < ArrayCount(g_coff_machine_map));
Assert(machine == g_coff_machine_map[machine].machine);
String8 string = g_coff_machine_map[machine].string;
return string;
}
internal String8
@@ -1117,3 +1342,36 @@ coff_string_from_section_flags(Arena *arena, COFF_SectionFlags flags)
return result;
}
internal String8
coff_string_from_import_header_type(COFF_ImportHeaderType type)
{
for (U64 i = 0; i < ArrayCount(g_coff_import_header_type_map); ++i) {
if (g_coff_import_header_type_map[i].type == type) {
return str8_cstring(g_coff_import_header_type_map[i].name);
}
}
return str8(0,0);
}
internal COFF_MachineType
coff_machine_from_string(String8 string)
{
for (U64 i = 0; i < ArrayCount(g_coff_machine_map); ++i) {
if (str8_match(g_coff_machine_map[i].string, string, StringMatchFlag_CaseInsensitive)) {
return g_coff_machine_map[i].machine;
}
}
return COFF_MachineType_UNKNOWN;
}
internal COFF_ImportHeaderType
coff_import_header_type_from_string(String8 name)
{
for (U64 i = 0; i < ArrayCount(g_coff_import_header_type_map); ++i) {
if (str8_match(str8_cstring(g_coff_import_header_type_map[i].name), name, StringMatchFlag_CaseInsensitive)) {
return g_coff_import_header_type_map[i].type;
}
}
return COFF_ImportHeaderType_COUNT;
}
+55 -7
View File
@@ -556,6 +556,11 @@ typedef U32 COFF_FeatFlags;
#define COFF_RES_ALIGN 4u
read_only U8 g_coff_res_magic[] = {
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
typedef struct COFF_ResourceHeaderPrefix
{
U32 data_size;
@@ -643,14 +648,12 @@ typedef struct COFF_ResourceDirEntry
////////////////////////////////
// !<arch>\n
#define COFF_ARCHIVE_SIG 0x0A3E686372613C21ULL
// !<thin>\n
#define COFF_THIN_ARCHIVE_SIG 0xA3E6E6968743C21ULL
read_only global U8 g_coff_archive_sig[8] = "!<arch>\n";
read_only global U8 g_coff_thin_archive_sig[8] = "!<thin>\n";
#define COFF_ARCHIVE_ALIGN 2
#define COFF_ARCHIVE_MAX_SHORT_NAME_SIZE 15
#define COFF_ARCHIVE_ALIGN 2
#define COFF_ARCHIVE_MEMBER_HEADER_SIZE 60
typedef struct COFF_ArchiveMemberHeader
{
@@ -817,6 +820,14 @@ typedef struct COFF_ArchiveParse
String8 long_names;
} COFF_ArchiveParse;
////////////////////////////////
typedef struct COFF_SectionHeaderArray
{
U64 count;
COFF_SectionHeader *v;
} COFF_SectionHeaderArray;
////////////////////////////////
//~ rjf: Globals
@@ -844,18 +855,48 @@ internal COFF_Symbol32Array coff_symbol_array_from_data(Arena *arena, String8 d
internal COFF_Symbol16Node * coff_symbol16_list_push(Arena *arena, COFF_Symbol16List *list, COFF_Symbol16 symbol);
internal COFF_RelocInfo coff_reloc_info_from_section_header(String8 data, COFF_SectionHeader *header);
internal U64 coff_word_size_from_machine(COFF_MachineType machine);
internal U64 coff_word_size_from_machine(COFF_MachineType machine);
internal U64 coff_default_exe_base_from_machine(COFF_MachineType machine);
internal U64 coff_default_dll_base_from_machine(COFF_MachineType machine);
internal String8 coff_make_import_lookup(Arena *arena, U16 hint, String8 name);
internal U32 coff_make_ordinal_32(U16 hint);
internal U64 coff_make_ordinal_64(U16 hint);
internal String8 coff_make_import_header_by_name(Arena *arena,
String8 dll_name,
COFF_MachineType machine,
COFF_TimeStamp time_stamp,
String8 name,
U16 hint,
COFF_ImportHeaderType type);
internal String8 coff_make_import_header_by_ordinal(Arena *arena,
String8 dll_name,
COFF_MachineType machine,
COFF_TimeStamp time_stamp,
U16 ordinal,
COFF_ImportHeaderType type);
internal B32 coff_resource_id_is_equal(COFF_ResourceID a, COFF_ResourceID b);
internal COFF_ResourceID coff_resource_id_copy(Arena *arena, COFF_ResourceID id);
internal U64 coff_sizeof_resource_id(COFF_ResourceID id);
internal COFF_ResourceID coff_convert_resource_id(Arena *arena, COFF_ResourceID_16 *id_16);
internal U64 coff_read_resource_id(String8 res, U64 off, COFF_ResourceID_16 *id_out);
internal U64 coff_read_resource(String8 data, U64 off, Arena *arena, COFF_Resource *res_out);
internal COFF_ResourceList coff_resource_list_from_data(Arena *arena, String8 data);
internal void coff_write_resource_id(Arena *arena, String8List *srl, COFF_ResourceID id);
internal String8List coff_write_resource(Arena *arena,
COFF_ResourceID type,
COFF_ResourceID name,
U32 data_version,
COFF_ResourceMemoryFlags memory_flags,
U16 language_id,
U32 version,
U32 characteristics,
String8 data);
internal COFF_DataType coff_data_type_from_data(String8 data);
internal B32 coff_is_import(String8 data);
internal B32 coff_is_archive(String8 data);
@@ -875,8 +916,15 @@ internal COFF_ArchiveParse coff_thin_archive_from_data(Arena *arena, String8 da
internal COFF_ArchiveType coff_archive_type_from_data(String8 data);
internal COFF_ArchiveParse coff_archive_parse_from_data(Arena *arena, String8 data);
////////////////////////////////
// String <-> Enum
internal String8 coff_string_from_comdat_select_type(COFF_ComdatSelectType select);
internal String8 coff_string_from_machine_type(COFF_MachineType machine);
internal String8 coff_string_from_section_flags(Arena *arena, COFF_SectionFlags flags);
internal String8 coff_string_from_import_header_type(COFF_ImportHeaderType type);
internal COFF_MachineType coff_machine_from_string(String8 string);
internal COFF_ImportHeaderType coff_import_header_type_from_string(String8 name);
#endif //COFF_H
+115 -58
View File
@@ -300,6 +300,14 @@ ctrl_serialized_string_from_msg_list(Arena *arena, CTRL_MsgList *msgs)
str8_serial_push_data(scratch.arena, &msgs_srlzed, n->string.str, n->string.size);
}
// rjf: write stdout/stderr/stdin paths
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->stdout_path.size);
str8_serial_push_string(scratch.arena, &msgs_srlzed, msg->stdout_path);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->stderr_path.size);
str8_serial_push_string(scratch.arena, &msgs_srlzed, msg->stderr_path);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->stdin_path.size);
str8_serial_push_string(scratch.arena, &msgs_srlzed, msg->stdin_path);
// rjf: write trap list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->traps.count);
for(CTRL_TrapNode *n = msg->traps.first; n != 0; n = n->next)
@@ -405,6 +413,17 @@ ctrl_msg_list_from_serialized_string(Arena *arena, String8 string)
str8_list_push(arena, &msg->env_string_list, env_str);
}
// rjf: read stdout/stderr/stdin paths
read_off += str8_deserial_read_struct(string, read_off, &msg->stdout_path.size);
msg->stdout_path.str = push_array(arena, U8, msg->stdout_path.size);
read_off += str8_deserial_read(string, read_off, msg->stdout_path.str, msg->stdout_path.size, 1);
read_off += str8_deserial_read_struct(string, read_off, &msg->stderr_path.size);
msg->stderr_path.str = push_array(arena, U8, msg->stderr_path.size);
read_off += str8_deserial_read(string, read_off, msg->stderr_path.str, msg->stderr_path.size, 1);
read_off += str8_deserial_read_struct(string, read_off, &msg->stdin_path.size);
msg->stdin_path.str = push_array(arena, U8, msg->stdin_path.size);
read_off += str8_deserial_read(string, read_off, msg->stdin_path.str, msg->stdin_path.size, 1);
// rjf: read trap list
U64 trap_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &trap_count);
@@ -3453,7 +3472,13 @@ ctrl_thread__module_open(CTRL_Handle process, CTRL_Handle module, Rng1U64 vaddr_
U64 pdatas_count = 0;
U64 entry_point_voff = 0;
Rng1U64 tls_vaddr_range = {0};
String8 builtin_debug_info_path = {0};
U32 pdb_dbg_time = 0;
U32 pdb_dbg_age = 0;
OS_Guid pdb_dbg_guid = {0};
String8 pdb_dbg_path = str8_zero();
U32 rdi_dbg_time = 0;
OS_Guid rdi_dbg_guid = {0};
String8 rdi_dbg_path = str8_zero();
ProfScope("unpack relevant PE info")
{
B32 is_valid = 1;
@@ -3589,65 +3614,62 @@ ctrl_thread__module_open(CTRL_Handle process, CTRL_Handle module, Rng1U64 vaddr_
tls_vaddr_range = r1u64(tls_header.index_address, tls_header.index_address+sizeof(U32));
// rjf: grab data about debug info
U32 dbg_time = 0;
U32 dbg_age = 0;
OS_Guid dbg_guid = {0};
if(data_dir_count > PE_DataDirectoryIndex_DEBUG)
{
// rjf: read data dir
PE_DataDirectory dir = {0};
dmn_process_read_struct(process.dmn_handle, vaddr_range.min + opt_ext_off_range.min + reported_data_dir_offset + sizeof(PE_DataDirectory)*PE_DataDirectoryIndex_DEBUG, &dir);
// rjf: read debug directory
PE_DebugDirectory dbg_data = {0};
dmn_process_read_struct(process.dmn_handle, vaddr_range.min+(U64)dir.virt_off, &dbg_data);
// rjf: extract external file info from codeview header
if(dbg_data.type == PE_DebugDirectoryType_CODEVIEW)
U64 dbg_dir_count = dir.virt_size / sizeof(PE_DebugDirectory);
for(U64 dbg_dir_idx = 0; dbg_dir_idx < dbg_dir_count; dbg_dir_idx += 1)
{
U64 dbg_path_off = 0;
U64 dbg_path_size = 0;
U64 cv_offset = dbg_data.voff;
U32 cv_magic = 0;
dmn_process_read_struct(process.dmn_handle, vaddr_range.min+cv_offset, &cv_magic);
switch(cv_magic)
// rjf: read debug directory
U64 dir_addr = vaddr_range.min + dir.virt_off + dbg_dir_idx * sizeof(PE_DebugDirectory);
PE_DebugDirectory dbg_data = {0};
dmn_process_read_struct(process.dmn_handle, dir_addr, &dbg_data);
// rjf: extract external file info from codeview header
if(dbg_data.type == PE_DebugDirectoryType_CODEVIEW)
{
default:break;
case PE_CODEVIEW_PDB20_MAGIC:
U32 cv_magic = 0;
dmn_process_read_struct(process.dmn_handle, vaddr_range.min + dbg_data.voff, &cv_magic);
switch(cv_magic)
{
PE_CvHeaderPDB20 cv = {0};
dmn_process_read_struct(process.dmn_handle, vaddr_range.min+cv_offset, &cv);
dbg_time = cv.time;
dbg_age = cv.age;
dbg_path_off = cv_offset + sizeof(cv);
}break;
case PE_CODEVIEW_PDB70_MAGIC:
{
PE_CvHeaderPDB70 cv = {0};
dmn_process_read_struct(process.dmn_handle, vaddr_range.min+cv_offset, &cv);
dbg_guid = cv.guid;
dbg_age = cv.age;
dbg_path_off = cv_offset + sizeof(cv);
}break;
}
if(dbg_path_off > 0)
{
Temp scratch = scratch_begin(0, 0);
String8List parts = {0};
for(U64 off = dbg_path_off;; off += 256)
{
U8 bytes[256] = {0};
dmn_process_read(process.dmn_handle, r1u64(vaddr_range.min+off, vaddr_range.min+off+sizeof(bytes)), bytes);
U64 size = cstring8_length(&bytes[0]);
String8 part = str8(bytes, size);
str8_list_push(scratch.arena, &parts, part);
if(size < sizeof(bytes))
default:break;
case PE_CODEVIEW_PDB20_MAGIC:
{
break;
}
PE_CvHeaderPDB20 cv;
U64 read_size = dmn_process_read_struct(process.dmn_handle, vaddr_range.min+dbg_data.voff, &cv);
if(read_size == sizeof(cv))
{
pdb_dbg_time = cv.time;
pdb_dbg_age = cv.age;
pdb_dbg_path = dmn_process_read_cstring(arena, process.dmn_handle, vaddr_range.min + dbg_data.voff + sizeof(cv));
}
}break;
case PE_CODEVIEW_PDB70_MAGIC:
{
PE_CvHeaderPDB70 cv;
U64 read_size = dmn_process_read_struct(process.dmn_handle, vaddr_range.min + dbg_data.voff, &cv);
if(read_size == sizeof(cv))
{
pdb_dbg_guid = cv.guid;
pdb_dbg_age = cv.age;
pdb_dbg_path = dmn_process_read_cstring(arena, process.dmn_handle, vaddr_range.min + dbg_data.voff + sizeof(cv));
}
}break;
case PE_CODEVIEW_RDI_MAGIC:
{
PE_CvHeaderRDI cv;
U64 read_size = dmn_process_read_struct(process.dmn_handle, vaddr_range.min + dbg_data.voff, &cv);
if(read_size == sizeof(cv))
{
rdi_dbg_guid = cv.guid;
rdi_dbg_path = dmn_process_read_cstring(arena, process.dmn_handle, vaddr_range.min + dbg_data.voff + sizeof(cv));
}
}break;
}
builtin_debug_info_path = str8_list_join(arena, &parts, 0);
scratch_end(scratch);
}
}
}
@@ -3657,16 +3679,22 @@ ctrl_thread__module_open(CTRL_Handle process, CTRL_Handle module, Rng1U64 vaddr_
//////////////////////////////
//- rjf: pick default initial debug info path
//
String8 initial_debug_info_path = builtin_debug_info_path;
String8 initial_debug_info_path = str8_zero();
{
Temp scratch = scratch_begin(0, 0);
String8 exe_folder = str8_chop_last_slash(path);
String8 builtin_debug_info_path__absolute = builtin_debug_info_path;
String8 builtin_debug_info_path__relative = push_str8f(scratch.arena, "%S/%S", exe_folder, builtin_debug_info_path);
String8 rdi_path__absolute = rdi_dbg_path;
String8 rdi_path__relative = push_str8f(scratch.arena, "%S/%S", exe_folder, rdi_dbg_path);
String8 pdb_path__absolute = pdb_dbg_path;
String8 pdb_path__relative = push_str8f(scratch.arena, "%S/%S", exe_folder, pdb_dbg_path);
String8 dbg_path_candidates[] =
{
/* inferred (treated as relative): */ builtin_debug_info_path__relative,
/* inferred (treated as absolute): */ builtin_debug_info_path__absolute,
/* inferred (treated as absolute): */ rdi_path__absolute,
/* inferred (treated as relative): */ rdi_path__relative,
/* inferred (treated as absolute): */ pdb_path__absolute,
/* inferred (treated as relative): */ pdb_path__relative,
/* "foo.exe" -> "foo.rdi" */ push_str8f(scratch.arena, "%S.rdi", str8_chop_last_dot(path)),
/* "foo.exe" -> "foo.exe.rdi" */ push_str8f(scratch.arena, "%S.rdi", path),
/* "foo.exe" -> "foo.pdb" */ push_str8f(scratch.arena, "%S.pdb", str8_chop_last_dot(path)),
/* "foo.exe" -> "foo.exe.pdb" */ push_str8f(scratch.arena, "%S.pdb", path),
};
@@ -4249,16 +4277,45 @@ ctrl_thread__end_and_flush_info_log(void)
internal void
ctrl_thread__launch(DMN_CtrlCtx *ctrl_ctx, CTRL_Msg *msg)
{
//- rjf: obtain stdout/stderr/stdin handles
OS_Handle stdout_handle = {0};
OS_Handle stderr_handle = {0};
OS_Handle stdin_handle = {0};
if(msg->stdout_path.size != 0)
{
OS_Handle f = os_file_open(OS_AccessFlag_Write|OS_AccessFlag_Read, msg->stdout_path);
os_file_close(f);
stdout_handle = os_file_open(OS_AccessFlag_Write|OS_AccessFlag_Append|OS_AccessFlag_ShareRead|OS_AccessFlag_ShareWrite|OS_AccessFlag_Inherited, msg->stdout_path);
}
if(msg->stderr_path.size != 0)
{
OS_Handle f = os_file_open(OS_AccessFlag_Write|OS_AccessFlag_Read, msg->stderr_path);
os_file_close(f);
stderr_handle = os_file_open(OS_AccessFlag_Write|OS_AccessFlag_Append|OS_AccessFlag_ShareRead|OS_AccessFlag_ShareWrite|OS_AccessFlag_Inherited, msg->stderr_path);
}
if(msg->stdin_path.size != 0)
{
stdin_handle = os_file_open(OS_AccessFlag_Read|OS_AccessFlag_ShareRead|OS_AccessFlag_ShareWrite|OS_AccessFlag_Inherited, msg->stdin_path);
}
//- rjf: launch
OS_ProcessLaunchParams params = {0};
{
params.cmd_line = msg->cmd_line_string_list;
params.path = msg->path;
params.env = msg->env_string_list;
params.inherit_env = msg->env_inherit;
params.cmd_line = msg->cmd_line_string_list;
params.path = msg->path;
params.env = msg->env_string_list;
params.inherit_env = msg->env_inherit;
params.stdout_file = stdout_handle;
params.stderr_file = stderr_handle;
params.stdin_file = stdin_handle;
}
U32 id = dmn_ctrl_launch(ctrl_ctx, &params);
//- rjf: close stdout/stderr/stdin files
os_file_close(stdout_handle);
os_file_close(stderr_handle);
os_file_close(stdin_handle);
//- rjf: record (id -> entry points), so that we know custom entry points for this PID
for(String8Node *n = msg->entry_points.first; n != 0; n = n->next)
{
+9
View File
@@ -86,6 +86,9 @@ Y(String8, type(CTRL_PathString8), dbg, "Debug Info Path")\
Y(String8, type(CTRL_PlainString8), args, "Arguments")\
Y(String8, type(CTRL_PathString8), working_directory, "Working Directory")\
Y(String8, type(CTRL_CodeString8), entry_point, "Custom Entry Point")\
Y(String8, type(CTRL_PathString8), stdout_path, "Standard Output Path")\
Y(String8, type(CTRL_PathString8), stderr_path, "Standard Error Path")\
Y(String8, type(CTRL_PathString8), stdin_path, "Standard Input Path")\
Y(String8, type(CTRL_PathString8), source_location, "Source Location")\
Y(String8, type(CTRL_CodeString8), function_location, "Function Location")\
Y(String8, type(CTRL_CodeString8), address_location, "Address Location")\
@@ -132,6 +135,9 @@ struct_members(CTRL_TargetMetaEval)
member_lit_comp(CTRL_MetaEval, type(CTRL_PlainString8),args, .pretty_name = str8_lit_comp("Arguments")),
member_lit_comp(CTRL_MetaEval, type(CTRL_PathString8), working_directory, .pretty_name = str8_lit_comp("Working Directory")),
member_lit_comp(CTRL_MetaEval, type(CTRL_CodeString8), entry_point, .pretty_name = str8_lit_comp("Custom Entry Point")),
member_lit_comp(CTRL_MetaEval, type(CTRL_PathString8), stdout_path, .pretty_name = str8_lit_comp("Standard Output Path")),
member_lit_comp(CTRL_MetaEval, type(CTRL_PathString8), stderr_path, .pretty_name = str8_lit_comp("Standard Error Path")),
member_lit_comp(CTRL_MetaEval, type(CTRL_PathString8), stdin_path, .pretty_name = str8_lit_comp("Standard Input Path")),
};
struct_members(CTRL_PinMetaEval)
@@ -544,6 +550,9 @@ struct CTRL_Msg
String8List entry_points;
String8List cmd_line_string_list;
String8List env_string_list;
String8 stdout_path;
String8 stderr_path;
String8 stdin_path;
CTRL_TrapList traps;
CTRL_UserBreakpointList user_bps;
CTRL_MetaEvalArray meta_evals;
+3 -3
View File
@@ -29,9 +29,9 @@ D_CmdTable: // | | | |
{RunToLine 0 1 Null null Nil Null 0 0 0 0 0 0 Play "run_to_line" "Run To Line" "Runs until a particular source line is hit." "" }
{RunToAddress 1 1 Vaddr null Nil Null 0 0 0 0 1 1 PlayStepForward "run_to_address" "Run To Address" "Runs until a particular address is hit." "" }
{Run 1 1 Null null Nil Null 0 0 0 0 0 0 Play "run" "Run" "Runs all targets after starting them if they have not been started yet." "play" }
{Restart 1 1 Null null Nil Null 0 0 0 0 0 0 Redo "restart" "Restart" "Kills all attached processes, then restarts the targets which were used to launch all current processes (if any).""restart,retry" }
{StepInto 1 1 Null null Nil Null 0 0 0 0 0 0 StepInto "step_into" "Step Into" "Steps once, possibly into function calls, for either source lines or instructions (whichever is selected)." "" }
{StepOver 1 1 Null null Nil Null 0 0 0 0 0 0 StepOver "step_over" "Step Over" "Steps once, always over function calls, for either source lines or instructions." "" }
{Restart 1 1 Null null Nil Null 0 0 0 0 0 0 Redo "restart" "Restart" "Kills all attached processes, then launches all active targets." "restart,retry" }
{StepInto 1 1 Null null Nil Null 0 0 0 0 0 0 StepInto "step_into" "Step Into" "Steps once, possibly into function calls, for either source lines or instructions (whichever is selected)." "" }
{StepOver 1 1 Null null Nil Null 0 0 0 0 0 0 StepOver "step_over" "Step Over" "Steps once, always over function calls, for either source lines or instructions." "" }
//- rjf: debug control context management operations
{FreezeThread 1 1 Thread null Nil Thread 0 0 0 0 0 1 Locked "freeze_thread" "Freeze Thread" "Freezes the passed thread." "callstack,unwind" }
+24 -4
View File
@@ -951,6 +951,7 @@ d_lines_array_from_file_path_line_range(Arena *arena, String8 file_path, Rng1S64
array.v = push_array(arena, D_LineList, array.count);
}
Temp scratch = scratch_begin(&arena, 1);
U64 *lines_num_voffs = push_array(scratch.arena, U64, array.count);
DI_Scope *scope = di_scope_open();
DI_KeyList dbgi_keys = d_push_active_dbgi_key_list(scratch.arena);
String8List overrides = rd_possible_overrides_from_file_path(scratch.arena, file_path);
@@ -971,7 +972,7 @@ d_lines_array_from_file_path_line_range(Arena *arena, String8 file_path, Rng1S64
// rjf: file_path_normalized * rdi -> src_id
B32 good_src_id = 0;
U32 src_id = 0;
if(rdi != &di_rdi_parsed_nil)
if(rdi != &di_rdi_parsed_nil) ProfScope("file_path_normalized * rdi -> src_id")
{
RDI_NameMap *mapptr = rdi_element_from_name_idx(rdi, NameMaps, RDI_NameMapKind_NormalSourcePaths);
RDI_ParsedNameMap map = {0};
@@ -990,7 +991,7 @@ d_lines_array_from_file_path_line_range(Arena *arena, String8 file_path, Rng1S64
}
// rjf: good src-id -> look up line info for visible range
if(good_src_id)
if(good_src_id) ProfScope("good src-id -> look up line info for visible range")
{
RDI_SourceFile *src = rdi_element_from_name_idx(rdi, SourceFiles, src_id);
RDI_SourceLineMap *src_line_map = rdi_element_from_name_idx(rdi, SourceLineMaps, src->source_line_map_idx);
@@ -1004,7 +1005,7 @@ d_lines_array_from_file_path_line_range(Arena *arena, String8 file_path, Rng1S64
D_LineList *list = &array.v[line_idx];
U32 voff_count = 0;
U64 *voffs = rdi_line_voffs_from_num(&line_map, u32_from_u64_saturate((U64)line_num), &voff_count);
for(U64 idx = 0; idx < voff_count; idx += 1)
if(lines_num_voffs[line_idx] < 8) ProfScope("iterate voffs (%i)", voff_count) for(U64 idx = 0; idx < voff_count; idx += 1)
{
U64 base_voff = voffs[idx];
U64 unit_idx = rdi_vmap_idx_from_section_kind_voff(rdi, RDI_SectionKind_UnitVMap, base_voff);
@@ -1024,6 +1025,11 @@ d_lines_array_from_file_path_line_range(Arena *arena, String8 file_path, Rng1S64
n->v.dbgi_key = key;
SLLQueuePush(list->first, list->last, n);
list->count += 1;
lines_num_voffs[line_idx] += 1;
if(lines_num_voffs[line_idx] >= 8)
{
break;
}
}
}
}
@@ -2031,6 +2037,9 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
String8 args = str8_skip_chop_whitespace(target->args);
String8 working_directory = str8_skip_chop_whitespace(target->working_directory);
String8 custom_entry_point_name = str8_skip_chop_whitespace(target->custom_entry_point_name);
String8 stdout_path = str8_skip_chop_whitespace(target->stdout_path);
String8 stderr_path = str8_skip_chop_whitespace(target->stderr_path);
String8 stdin_path = str8_skip_chop_whitespace(target->stdin_path);
String8List env = target->env;
if(working_directory.size == 0)
{
@@ -2071,6 +2080,9 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
msg->kind = CTRL_MsgKind_Launch;
msg->path = working_directory;
msg->cmd_line_string_list = cmdln_strings;
msg->stdout_path = stdout_path;
msg->stderr_path = stderr_path;
msg->stdin_path = stdin_path;
msg->env_inherit = 1;
MemoryCopyArray(msg->exception_code_filters, exception_code_filters);
MemoryCopyStruct(&msg->meta_evals, meta_evals);
@@ -2089,7 +2101,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
// rjf: no targets -> error
if(targets_to_launch->count == 0)
{
log_user_error(str8_lit("No active targets exist; cannot launch. You must select target first."));
log_user_error(str8_lit("No active targets exist; cannot launch. You must select a target first."));
}
}break;
case D_CmdKind_Kill:
@@ -2375,6 +2387,14 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
msg->entity = e->handle;
}
}
if(d_ctrl_targets_running())
{
need_run = 1;
run_kind = d_state->ctrl_last_run_kind;
run_thread = ctrl_entity_from_handle(d_state->ctrl_entity_store, d_state->ctrl_last_run_thread_handle);
run_flags = d_state->ctrl_last_run_flags;
run_traps = d_state->ctrl_last_run_traps;
}
}break;
//- rjf: entity decoration
+3
View File
@@ -14,6 +14,9 @@ struct D_Target
String8 args;
String8 working_directory;
String8 custom_entry_point_name;
String8 stdout_path;
String8 stderr_path;
String8 stdin_path;
String8List env;
};
+30
View File
@@ -156,3 +156,33 @@ dmn_rsp_from_thread(DMN_Handle thread)
scratch_end(scratch);
return result;
}
////////////////////////////////
//~ Memory Helpers
internal String8
dmn_process_read_cstring(Arena *arena, DMN_Handle process, U64 addr)
{
Temp scratch = scratch_begin(&arena, 1);
String8List block_list = {0};
for(U64 cursor = addr, stride = 256; ; cursor += stride)
{
U8 *raw_block = push_array_no_zero(scratch.arena, U8, stride);
U64 read_size = dmn_process_read(process, r1u64(cursor, cursor + stride), raw_block);
String8 block = str8_cstring_capped(raw_block, raw_block + read_size);
str8_list_push(scratch.arena, &block_list, block);
if(read_size != stride || (block.size+1 <= read_size && block.str[block.size] == 0))
{
break;
}
}
String8 result = str8_list_join(arena, &block_list, 0);
scratch_end(scratch);
return result;
}
+1
View File
@@ -226,6 +226,7 @@ internal U64 dmn_process_read(DMN_Handle process, Rng1U64 range, void *dst);
internal B32 dmn_process_write(DMN_Handle process, Rng1U64 range, void *src);
#define dmn_process_read_struct(process, vaddr, ptr) dmn_process_read((process), r1u64((vaddr), (vaddr)+(sizeof(*ptr))), ptr)
#define dmn_process_write_struct(process, vaddr, ptr) dmn_process_write((process), r1u64((vaddr), (vaddr)+(sizeof(*ptr))), ptr)
internal String8 dmn_process_read_cstring(Arena *arena, DMN_Handle process, U64 addr);
//- rjf: threads
internal Arch dmn_arch_from_thread(DMN_Handle handle);
+24 -2
View File
@@ -1256,11 +1256,33 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
String16 env16 = str16_from_8(scratch.arena, env);
//- rjf: launch
DWORD access_flags = CREATE_UNICODE_ENVIRONMENT|DEBUG_PROCESS;
DWORD creation_flags = CREATE_UNICODE_ENVIRONMENT|DEBUG_PROCESS;
BOOL inherit_handles = 0;
STARTUPINFOW startup_info = {sizeof(startup_info)};
if(!os_handle_match(params->stdout_file, os_handle_zero()))
{
HANDLE stdout_handle = (HANDLE)params->stdout_file.u64[0];
startup_info.hStdOutput = stdout_handle;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
inherit_handles = 1;
}
if(!os_handle_match(params->stderr_file, os_handle_zero()))
{
HANDLE stderr_handle = (HANDLE)params->stderr_file.u64[0];
startup_info.hStdError = stderr_handle;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
inherit_handles = 1;
}
if(!os_handle_match(params->stdin_file, os_handle_zero()))
{
HANDLE stdin_handle = (HANDLE)params->stdin_file.u64[0];
startup_info.hStdInput = stdin_handle;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
inherit_handles = 1;
}
PROCESS_INFORMATION process_info = {0};
AllocConsole();
if(CreateProcessW(0, (WCHAR*)cmd16.str, 0, 0, 1, access_flags, (WCHAR*)env16.str, (WCHAR*)dir16.str, &startup_info, &process_info))
if(CreateProcessW(0, (WCHAR*)cmd16.str, 0, 0, 1, creation_flags, (WCHAR*)env16.str, (WCHAR*)dir16.str, &startup_info, &process_info))
{
// check if we are 32-bit app, and just close it immediately
BOOL is_wow = 0;
+1
View File
@@ -1050,6 +1050,7 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
ptr_root = e_irtree_resolve_to_value(arena, ptr_tree->space, ptr_tree->mode, ptr_root, ptr_tree->type_key);
}
E_IRNode *int_root = int_tree->root;
int_root = e_irtree_resolve_to_value(arena, int_tree->space, int_tree->mode, int_root, int_tree->type_key);
if(direct_type_size > 1)
{
E_IRNode *const_root = e_irtree_const_u(arena, direct_type_size);
+6
View File
@@ -1018,6 +1018,12 @@ e_parse_type_from_text_tokens(Arena *arena, String8 text, E_TokenArray *tokens)
if(token.kind == E_TokenKind_Identifier)
{
String8 token_string = str8_substr(text, token.range);
if(token_string.size >= 2 &&
token_string.str[0] == '`' &&
token_string.str[token_string.size-1] == '`')
{
token_string = str8_substr(token_string, r1u64(1, token_string.size-1));
}
E_TypeKey type_key = e_leaf_type_from_name(token_string);
if(!e_type_key_match(e_type_key_zero(), type_key))
{
+2
View File
@@ -565,6 +565,7 @@ e_type_kind_from_key(E_TypeKey key)
internal E_Type *
e_type_from_key(Arena *arena, E_TypeKey key)
{
ProfBeginFunction();
E_Type *type = &e_type_nil;
U64 reg_byte_count = 0;
{
@@ -1115,6 +1116,7 @@ e_type_from_key(Arena *arena, E_TypeKey key)
}break;
}
}
ProfEnd();
return type;
}
@@ -104,6 +104,7 @@ EV_ViewRuleTable:
{x Oct oct "oct" x - - - "Display In Octal" x "" "Specifies that all numeric values should be shown in base 8 (octal)." }
{x Dec dec "dec" x - - - "Display In Decimal" x "" "Specifies that all numeric values should be shown in base 10 (decimal)." }
{x Hex hex "hex" x - - - "Display In Hexadecimal" x "" "Specifies that all numeric values should be shown in base 16 (hexadecimal)." }
{x NoAddress no_addr "no_addr" x - - - "Omit Addresses" x "" "Specifies that addresses should be omitted from visualizations, if possible." }
}
@enum EV_ViewRuleKind:
@@ -671,6 +671,7 @@ ev_resolved_from_expr(Arena *arena, E_Expr *expr, EV_ViewRuleList *view_rules)
internal EV_BlockTree
ev_block_tree_from_expr(Arena *arena, EV_View *view, String8 filter, String8 string, E_Expr *expr, EV_ViewRuleList *view_rules)
{
ProfBeginFunction();
EV_BlockTree tree = {&ev_nil_block};
{
Temp scratch = scratch_begin(&arena, 1);
@@ -840,46 +841,51 @@ ev_block_tree_from_expr(Arena *arena, EV_View *view, String8 filter, String8 str
{
continue;
}
EV_ExpandRangeInfo child_expand = expand_view_rule_info->expr_expand_range_info(arena, view, filter, t->expr, expand_params, r1u64(split_relative_idx, split_relative_idx+1), expand_info.user_data);
if(child_expand.row_exprs_count > 0)
if(expand_info.rows_default_expanded || ev_expansion_from_key(view, child_keys[idx]))
{
EV_Key child_key = child_keys[idx];
E_Expr *child_expr = child_expand.row_exprs[0];
EV_ViewRuleList *child_view_rules = ev_view_rule_list_from_inheritance(arena, t->view_rules);
String8 child_view_rule_string = ev_view_rule_from_key(view, child_key);
ev_view_rule_list_push_string(arena, child_view_rules, child_view_rule_string);
if(child_expand.row_strings[0].size != 0)
EV_ExpandRangeInfo child_expand = expand_view_rule_info->expr_expand_range_info(arena, view, filter, t->expr, expand_params, r1u64(split_relative_idx, split_relative_idx+1), expand_info.user_data);
if(child_expand.row_exprs_count > 0)
{
EV_ViewRuleList *fastpath_view_rules = ev_view_rule_list_from_expr_fastpaths(arena, child_expand.row_strings[0]);
ev_view_rule_list_concat_in_place(child_view_rules, &fastpath_view_rules);
EV_Key child_key = child_keys[idx];
E_Expr *child_expr = child_expand.row_exprs[0];
EV_ViewRuleList *child_view_rules = ev_view_rule_list_from_inheritance(arena, t->view_rules);
String8 child_view_rule_string = ev_view_rule_from_key(view, child_key);
ev_view_rule_list_push_string(arena, child_view_rules, child_view_rule_string);
if(child_expand.row_strings[0].size != 0)
{
EV_ViewRuleList *fastpath_view_rules = ev_view_rule_list_from_expr_fastpaths(arena, child_expand.row_strings[0]);
ev_view_rule_list_concat_in_place(child_view_rules, &fastpath_view_rules);
}
{
Temp scratch = scratch_begin(&arena, 1);
E_IRTreeAndType child_irtree = e_irtree_and_type_from_expr(scratch.arena, child_expr);
EV_ViewRuleList *child_auto_view_rules = ev_auto_view_rules_from_type_key(arena, child_irtree.type_key, 1, child_view_rule_string.size == 0);
ev_view_rule_list_concat_in_place(child_view_rules, &child_auto_view_rules);
scratch_end(scratch);
}
E_Expr *child_expr__resolved = ev_resolved_from_expr(arena, child_expr, child_view_rules);
Task *task = push_array(scratch.arena, Task, 1);
SLLQueuePush(first_task, last_task, task);
task->parent_block = expansion_block;
task->expr = child_expr__resolved;
task->child_id = child_key.child_id;
task->view_rules = child_view_rules;
task->split_relative_idx = split_relative_idx;
task->default_expanded = expand_info.rows_default_expanded;
}
{
Temp scratch = scratch_begin(&arena, 1);
E_IRTreeAndType child_irtree = e_irtree_and_type_from_expr(scratch.arena, child_expr);
EV_ViewRuleList *child_auto_view_rules = ev_auto_view_rules_from_type_key(arena, child_irtree.type_key, 1, child_view_rule_string.size == 0);
ev_view_rule_list_concat_in_place(child_view_rules, &child_auto_view_rules);
scratch_end(scratch);
}
E_Expr *child_expr__resolved = ev_resolved_from_expr(arena, child_expr, child_view_rules);
Task *task = push_array(scratch.arena, Task, 1);
SLLQueuePush(first_task, last_task, task);
task->parent_block = expansion_block;
task->expr = child_expr__resolved;
task->child_id = child_key.child_id;
task->view_rules = child_view_rules;
task->split_relative_idx = split_relative_idx;
task->default_expanded = expand_info.rows_default_expanded;
}
}
}
scratch_end(scratch);
}
ProfEnd();
return tree;
}
internal EV_BlockTree
ev_block_tree_from_string(Arena *arena, EV_View *view, String8 filter, String8 string, EV_ViewRuleList *view_rules)
{
ProfBeginFunction();
EV_BlockTree tree = {0};
Temp scratch = scratch_begin(&arena, 1);
{
@@ -891,6 +897,7 @@ ev_block_tree_from_string(Arena *arena, EV_View *view, String8 filter, String8 s
tree = ev_block_tree_from_expr(arena, view, filter, string, parse.expr, all_view_rules);
}
scratch_end(scratch);
ProfEnd();
return tree;
}
@@ -1591,15 +1598,16 @@ ev_string_from_simple_typed_eval(Arena *arena, EV_StringFlags flags, U32 radix,
break;
}
}
String8 numeric_value_string = str8_from_u64(scratch.arena, eval.value.u64, radix, min_digits, digit_group_separator);
if(flags & EV_StringFlag_ReadOnlyDisplayRules)
{
if(constant_name.size != 0)
{
result = push_str8f(arena, "0x%I64x (%S)", eval.value.u64, constant_name);
result = push_str8f(arena, "%S (%S)", numeric_value_string, constant_name);
}
else
{
result = push_str8f(arena, "0x%I64x (%I64u)", eval.value.u64, eval.value.u64);
result = push_str8_copy(arena, numeric_value_string);
}
}
else if(constant_name.size != 0)
@@ -1608,7 +1616,7 @@ ev_string_from_simple_typed_eval(Arena *arena, EV_StringFlags flags, U32 radix,
}
else
{
result = push_str8f(arena, "0x%I64x (%I64u)", eval.value.u64, eval.value.u64);
result = push_str8_copy(arena, numeric_value_string);
}
scratch_end(scratch);
}break;
@@ -4,7 +4,7 @@
//- GENERATED CODE
C_LINKAGE_BEGIN
EV_ViewRuleInfo ev_builtin_view_rule_info_table[13] =
EV_ViewRuleInfo ev_builtin_view_rule_info_table[14] =
{
{str8_lit_comp("default"), (EV_ViewRuleInfoFlag_Inherited*0)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(default) , EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(default) , EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
{str8_lit_comp("array"), (EV_ViewRuleInfoFlag_Inherited*0)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(array) , EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
@@ -19,6 +19,7 @@ EV_ViewRuleInfo ev_builtin_view_rule_info_table[13] =
{str8_lit_comp("oct"), (EV_ViewRuleInfoFlag_Inherited*1)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
{str8_lit_comp("dec"), (EV_ViewRuleInfoFlag_Inherited*1)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
{str8_lit_comp("hex"), (EV_ViewRuleInfoFlag_Inherited*1)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
{str8_lit_comp("no_addr"), (EV_ViewRuleInfoFlag_Inherited*1)|(EV_ViewRuleInfoFlag_Expandable*0), EV_VIEW_RULE_EXPR_RESOLUTION_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_RANGE_INFO_FUNCTION_NAME(nil), EV_VIEW_RULE_EXPR_EXPAND_ID_FROM_NUM_FUNCTION_NAME(identity), EV_VIEW_RULE_EXPR_EXPAND_NUM_FROM_ID_FUNCTION_NAME(identity) },
};
C_LINKAGE_END
@@ -21,6 +21,7 @@ EV_ViewRuleKind_Bin,
EV_ViewRuleKind_Oct,
EV_ViewRuleKind_Dec,
EV_ViewRuleKind_Hex,
EV_ViewRuleKind_NoAddress,
EV_ViewRuleKind_COUNT,
} EV_ViewRuleKind;
+5 -1
View File
@@ -306,7 +306,8 @@ typedef enum RDI_LanguageEnum
RDI_Language_NULL = 0,
RDI_Language_C = 1,
RDI_Language_CPlusPlus = 2,
RDI_Language_COUNT = 3,
RDI_Language_Masm = 3,
RDI_Language_COUNT = 4,
} RDI_LanguageEnum;
typedef RDI_U16 RDI_TypeKind;
@@ -367,6 +368,7 @@ RDI_TypeKind_IncompleteClass = 0x2007,
RDI_TypeKind_IncompleteEnum = 0x2008,
RDI_TypeKind_Bitfield = 0xF000,
RDI_TypeKind_Variadic = 0xF001,
RDI_TypeKind_Count = 0xF002,
RDI_TypeKind_FirstBuiltIn = RDI_TypeKind_Void,
RDI_TypeKind_LastBuiltIn = RDI_TypeKind_ComplexF128,
RDI_TypeKind_FirstConstructed = RDI_TypeKind_Modifier,
@@ -655,6 +657,7 @@ X(RDI_U32, line_map_voff_base_idx)\
X(NULL)\
X(C)\
X(CPlusPlus)\
X(Masm)\
X(COUNT)\
#define RDI_TypeKind_XList \
@@ -713,6 +716,7 @@ X(IncompleteClass)\
X(IncompleteEnum)\
X(Bitfield)\
X(Variadic)\
X(Count)\
#define RDI_TypeModifierFlags_XList \
X(Const)\
+82 -29
View File
@@ -1129,7 +1129,7 @@ rdim_bake_vmap_from_markers(RDIM_Arena *arena, RDIM_VMapMarker *markers, RDIM_So
//- rjf: fill output vmap entries
RDI_U32 vmap_count_raw = marker_count - 1 + extra_vmap_entry;
RDI_VMapEntry *vmap = rdim_push_array_no_zero(arena, RDI_VMapEntry, vmap_count_raw + 1);
RDI_VMapEntry *vmap = rdim_push_array(arena, RDI_VMapEntry, vmap_count_raw + 1);
RDI_U32 vmap_entry_count_pass_1 = 0;
{
typedef struct RDIM_VMapRangeTracker RDIM_VMapRangeTracker;
@@ -1345,11 +1345,11 @@ rdim_bake_string_chunk_list_sorted_from_unsorted(RDIM_Arena *arena, RDIM_BakeStr
// build new sort tasks for buckets with many elements
{
RDI_U64 write_idx = 0;
for(U64 bucket_idx = 0; bucket_idx < 256; bucket_idx += 1)
for(RDI_U64 bucket_idx = 0; bucket_idx < 256; bucket_idx += 1)
{
// rjf: write each chunk node's array into original array, detect if there is size left to sort
RDI_U64 bucket_base_idx = write_idx;
RDI_U64 max_size_left_to_sort = 0;
RDI_U64 need_next_char_sort = 0;
for(RDIM_BakeStringChunkNode *n = buckets[bucket_idx].first; n != 0; n = n->next)
{
rdim_memcpy(t->v+write_idx, n->v, sizeof(n->v[0])*n->count);
@@ -1358,14 +1358,14 @@ rdim_bake_string_chunk_list_sorted_from_unsorted(RDIM_Arena *arena, RDIM_BakeStr
{
if(n->v[idx].string.size > t->string_off+1)
{
max_size_left_to_sort = Max(max_size_left_to_sort, (n->v[idx].string.size - t->string_off+1));
need_next_char_sort = 1;
}
}
}
// rjf: if any bucket has >1 element & has some amount of size left to sort, push new task for this
// bucket's region in the array, and for remainder of keys
if(buckets[bucket_idx].total_count > 1 && max_size_left_to_sort > 0)
if(buckets[bucket_idx].total_count > 1 && need_next_char_sort)
{
SortTask *new_task = rdim_push_array(scratch.arena, SortTask, 1);
RDIM_SLLQueuePush(first_task, last_task, new_task);
@@ -1376,9 +1376,60 @@ rdim_bake_string_chunk_list_sorted_from_unsorted(RDIM_Arena *arena, RDIM_BakeStr
}
}
}
scratch_end(scratch);
rdim_scratch_end(scratch);
}
//- rjf: iterate sorted chunk node, remove duplicates, count # of duplicates
RDI_U64 num_duplicates = 0;
if(dst.first != 0)
{
RDI_U64 last_idx = 0;
for(RDI_U64 idx = 1; idx < dst.first->count; idx += 1)
{
if(rdim_str8_match(dst.first->v[last_idx].string, dst.first->v[idx].string, 0))
{
rdim_memzero_struct(&dst.first->v[idx]);
num_duplicates += 1;
}
else
{
last_idx = idx;
}
}
}
//- rjf: iterate sorted chunk node, make non-empty elements contiguous
if(num_duplicates != 0)
{
RDI_U64 last_idx = 0;
for(RDI_U64 idx = 1; idx < dst.first->count; idx += 1)
{
if(last_idx == 0 &&
dst.first->v[idx].string.RDIM_String8_SizeMember == 0 &&
dst.first->v[idx].hash == 0)
{
last_idx = idx;
}
if(last_idx != 0 && dst.first->v[idx].string.RDIM_String8_SizeMember != 0)
{
rdim_memcpy_struct(&dst.first->v[last_idx], &dst.first->v[idx]);
rdim_memzero_struct(&dst.first->v[idx]);
last_idx += 1;
}
}
//- rjf: pop extras
if(num_duplicates != 0)
{
RDI_U64 arena_pos_pre_pop = rdim_arena_pos(arena);
rdim_arena_pop_to(arena, arena_pos_pre_pop - num_duplicates*sizeof(dst.first->v[0]));
dst.first->count -= num_duplicates;
dst.first->cap -= num_duplicates;
dst.total_count -= num_duplicates;
}
}
return dst;
}
@@ -1395,31 +1446,34 @@ rdim_bake_string_map_loose_make(RDIM_Arena *arena, RDIM_BakeStringMapTopology *t
RDI_PROC void
rdim_bake_string_map_loose_insert(RDIM_Arena *arena, RDIM_BakeStringMapTopology *map_topology, RDIM_BakeStringMapLoose *map, RDI_U64 chunk_cap, RDIM_String8 string)
{
RDI_U64 hash = rdi_hash(string.RDIM_String8_BaseMember, string.RDIM_String8_SizeMember);
RDI_U64 slot_idx = hash%map_topology->slots_count;
RDIM_BakeStringChunkList *slot = map->slots[slot_idx];
if(slot == 0)
if(string.RDIM_String8_SizeMember != 0)
{
slot = map->slots[slot_idx] = rdim_push_array(arena, RDIM_BakeStringChunkList, 1);
}
RDI_S32 is_duplicate = 0;
for(RDIM_BakeStringChunkNode *n = slot->first; n != 0; n = n->next)
{
for(RDI_U64 idx = 0; idx < n->count; idx += 1)
RDI_U64 hash = rdi_hash(string.RDIM_String8_BaseMember, string.RDIM_String8_SizeMember);
RDI_U64 slot_idx = hash%map_topology->slots_count;
RDIM_BakeStringChunkList *slot = map->slots[slot_idx];
if(slot == 0)
{
if(rdim_str8_match(n->v[idx].string, string, 0))
slot = map->slots[slot_idx] = rdim_push_array(arena, RDIM_BakeStringChunkList, 1);
}
RDI_S32 is_duplicate = 0;
for(RDIM_BakeStringChunkNode *n = slot->first; n != 0; n = n->next)
{
for(RDI_U64 idx = 0; idx < n->count; idx += 1)
{
is_duplicate = 1;
goto break_all;
if(rdim_str8_match(n->v[idx].string, string, 0))
{
is_duplicate = 1;
goto break_all;
}
}
}
}
break_all:;
if(!is_duplicate)
{
RDIM_BakeString *bstr = rdim_bake_string_chunk_list_push(arena, slot, chunk_cap);
bstr->string = string;
bstr->hash = hash;
break_all:;
if(!is_duplicate)
{
RDIM_BakeString *bstr = rdim_bake_string_chunk_list_push(arena, slot, chunk_cap);
bstr->string = string;
bstr->hash = hash;
}
}
}
@@ -3057,7 +3111,7 @@ rdim_bake_global_vmap(RDIM_Arena *arena, RDIM_SymbolChunkList *src)
RDIM_Temp scratch = rdim_scratch_begin(&arena, 1);
//- rjf: allocate keys/markers
RDI_U64 marker_count = src->total_count*2;
RDI_U64 marker_count = src->total_count*2 + 2;
RDIM_SortKey *keys = rdim_push_array_no_zero(scratch.arena, RDIM_SortKey, marker_count);
RDIM_VMapMarker *markers = rdim_push_array_no_zero(scratch.arena, RDIM_VMapMarker, marker_count);
@@ -3427,7 +3481,7 @@ rdim_bake_inline_sites(RDIM_Arena *arena, RDIM_BakeStringMapTight *strings, RDIM
{
RDIM_InlineSiteBakeResult result = {0};
{
result.inline_sites_count = src->total_count;
result.inline_sites_count = src->total_count+1;
result.inline_sites = rdim_push_array(arena, RDI_InlineSite, result.inline_sites_count+1);
RDI_U64 dst_idx = 1;
for(RDIM_InlineSiteChunkNode *n = src->first; n != 0; n = n->next)
@@ -3510,7 +3564,6 @@ rdim_bake_file_paths(RDIM_Arena *arena, RDIM_BakeStringMapTight *strings, RDIM_B
RDI_PROC RDIM_StringBakeResult
rdim_bake_strings(RDIM_Arena *arena, RDIM_BakeStringMapTight *strings)
{
RDIM_BakeSectionList sections = {0};
RDI_U32 *str_offs = rdim_push_array_no_zero(arena, RDI_U32, strings->total_count + 1);
RDI_U32 off_cursor = 0;
{
+88
View File
@@ -0,0 +1,88 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal String8
push_cstr(Arena *arena, String8 str)
{
U64 buffer_size = str.size + 1;
U8 *buffer = push_array_no_zero(arena, U8, buffer_size);
MemoryCopy(buffer, str.str, str.size);
buffer[str.size] = 0;
String8 result = str8(buffer, buffer_size);
return result;
}
internal U32 *
push_u32(Arena *arena, U32 value)
{
U32 *result = push_array_no_zero(arena, U32, 1);
*result = value;
return result;
}
internal U64 *
push_u64(Arena *arena, U64 value)
{
U64 *result = push_array_no_zero(arena, U64, 1);
*result = value;
return result;
}
internal U32 *
push_array_copy_u32(Arena *arena, U32 *v, U64 count)
{
U32 *result = push_array_no_zero(arena, U32, count);
MemoryCopyTyped(result, v, count);
return result;
}
internal U64 *
push_array_copy_u64(Arena *arena, U64 *v, U64 count)
{
U64 *result = push_array_no_zero(arena, U64, count);
MemoryCopyTyped(result, v, count);
return result;
}
internal U64 **
push_matrix_u64(Arena *arena, U64 rows, U64 columns)
{
U64 **result = push_array_no_zero(arena, U64 *, rows);
for (U64 row_idx = 0; row_idx < rows; row_idx += 1) {
result[row_idx] = push_array(arena, U64, columns);
}
return result;
}
internal Arena **
alloc_fixed_size_arena_array(Arena *arena, U64 count, U64 res, U64 cmt)
{
U64 data_size = sizeof(count) + sizeof(Arena *) * count;
U8 *data = push_array_no_zero(arena, U8, data_size);
U64 *count_ptr = (U64 *)data;
Arena **arr = (Arena **)(count_ptr + 1);
*count_ptr = count;
ArenaParams params = {0};
params.reserve_size = res;
params.commit_size = cmt;
for (U64 i = 0; i < count; i += 1) {
Arena *fixed_arena = arena_alloc_(&params);
arr[i] = fixed_arena;
}
return arr;
}
internal void
release_arena_array(Arena **arr)
{
U64 *count_ptr = (U64 *)arr - 1;
U64 count = *count_ptr;
for (U64 i = 0; i < count; i += 1) {
arena_release(arr[i]);
arr[i] = 0;
}
}
+15
View File
@@ -0,0 +1,15 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
internal U32 * push_u32(Arena *arena, U32 value);
internal U64 * push_u64(Arena *arena, U64 value);
internal U32 * push_array_copy_u32(Arena *arena, U32 *v, U64 count);
internal U64 * push_array_copy_u64(Arena *arena, U64 *v, U64 count);
internal U64 ** push_matrix_u64(Arena *arena, U64 rows, U64 columns);
internal String8 push_cstr(Arena *arena, String8 str);
internal Arena ** alloc_fixed_size_arena_array(Arena *arena, U64 count, U64 res, U64 cmt);
internal void release_arena_array(Arena **arr);
+230
View File
@@ -0,0 +1,230 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U64
void_list_count_nodes(VoidNode *head)
{
U64 node_count = 0;
for (VoidNode *curr = head; curr != 0; curr = curr->next) {
++node_count;
}
return node_count;
}
internal void
void_node_concat(VoidNode **head, VoidNode *node)
{
Assert(*head != node);
node->next = *head;
*head = node;
}
internal void
void_node_concat_atomic(VoidNode **head, VoidNode *node)
{
Assert(*head != node);
node->next = ins_atomic_ptr_eval_assign(head, node);
}
internal U64Node *
u64_list_push(Arena *arena, U64List *list, U64 data)
{
U64Node *n = push_array(arena, U64Node, 1);
n->next = 0;
n->data = data;
SLLQueuePush(list->first, list->last, n);
++list->count;
return n;
}
internal void
u64_list_concat_in_place(U64List *list, U64List *to_concat)
{
SLLConcatInPlace(list, to_concat);
}
internal U64Array
u64_array_from_list(Arena *arena, U64List *list)
{
U64Array result;
result.count = 0;
result.v = push_array(arena, U64, list->count);
for (U64Node *n = list->first; n != NULL; n = n->next) {
result.v[result.count++] = n->data;
}
return result;
}
internal void
u32_array_sort(U64 count, U32 *v)
{
radsort(v, count, u32_is_before);
}
internal void
u32_pair_radix_sort(U64 count, PairU32 *arr)
{
Temp scratch = scratch_begin(0,0);
PairU32 *temp = push_array(scratch.arena, PairU32, count);
const U64 bit_count0 = 11;
const U64 bit_count1 = 11;
const U64 bit_count2 = 10;
U32 *count0 = push_array(scratch.arena, U32, (1 << bit_count0));
U32 *count1 = push_array(scratch.arena, U32, (1 << bit_count1));
U32 *count2 = push_array(scratch.arena, U32, (1 << bit_count2));
for (U64 i = 0; i < count; ++i) {
U32 digit0 = (arr[i].v0 >> 0 ) % (1 << bit_count0);
U32 digit1 = (arr[i].v0 >> bit_count0) % (1 << bit_count1);
U32 digit2 = (arr[i].v0 >> bit_count1) % (1 << bit_count2);
++count0[digit0];
++count1[digit1];
++count2[digit2];
}
counts_to_offsets_array_u32((1 << bit_count0), count0);
counts_to_offsets_array_u32((1 << bit_count1), count1);
counts_to_offsets_array_u32((1 << bit_count2), count2);
for (U64 i = 0; i < count; ++i) {
U32 digit0 = (arr[i].v0 >> 0) % (1 << bit_count0);
temp[count0[digit0]++] = arr[i];
}
for (U64 i = 0; i < count; ++i) {
U32 digit1 = (temp[i].v0 >> bit_count0) % (1 << bit_count1);
arr[count1[digit1]++] = temp[i];
}
for (U64 i = 0; i < count; ++i) {
U32 digit2 = (arr[i].v0 >> bit_count1) % (1 << bit_count2);
temp[count2[digit2]++] = arr[i];
}
MemoryCopyTyped(arr, temp, count);
scratch_end(scratch);
}
internal B32
u32_array_compare(U32Array a, U32Array b)
{
B32 are_equal = 0;
if (a.count == b.count) {
int cmp = MemoryCompare(a.v, b.v, sizeof(a.v[0]) * a.count);
are_equal = (cmp == 0);
}
return are_equal;
}
internal U64Array
u64_array_remove_duplicates(Arena *arena, U64Array in)
{
U64Array result;
result.count = 0;
result.v = push_array(arena, U64, in.count);
for (U64 i = 1; i < in.count; ++i) {
B32 is_unique = in.v[i - 1] != in.v[i];
if (is_unique) {
result.v[result.count++] = in.v[i - 1];
}
}
if (in.count > 0 && result.count > 0) {
B32 is_unique = result.v[result.count - 1] != in.v[in.count - 1];
if (is_unique) {
result.v[result.count++] = in.v[in.count - 1];
}
}
U64 slack_size = (in.count - result.count) * sizeof(result.v[0]);
arena_pop(arena, slack_size);
return result;
}
internal U64
sum_array_u64(U64 count, U64 *v)
{
U64 result = 0;
for (U64 i = 0; i < count; i += 1) {
result += v[i];
}
return result;
}
internal U64
sum_matrix_u64(U64 rows, U64 cols, U64 **v)
{
U64 result = 0;
for (U64 i = 0; i < rows; ++i) {
result += sum_array_u64(cols, v[i]);
}
return result;
}
internal U64
max_array_u64(U64 count, U64 *v)
{
U64 result = 0;
for (U64 i = 0; i < count; i += 1) {
result = Max(v[i], result);
}
return result;
}
internal U64
min_array_u64(U64 count, U64 *v)
{
U64 result = max_U64;
for (U64 i = 0; i < count; i += 1) {
result = Min(v[i], result);
}
return result;
}
internal void
counts_to_offsets_array_u32(U64 count, U32 *arr)
{
U32 next_offset = 0;
for (U64 i = 0; i < count; i += 1) {
U32 current_offset = next_offset;
next_offset += arr[i];
arr[i] = current_offset;
}
}
internal void
counts_to_offsets_array_u64(U64 count, U64 *arr)
{
U64 next_offset = 0;
for (U64 i = 0; i < count; i += 1) {
U64 current_offset = next_offset;
next_offset += arr[i];
arr[i] = current_offset;
}
}
internal U32 *
offsets_from_counts_array_u32(Arena *arena, U32 *v, U64 count)
{
U32 *result = push_array_copy_u32(arena, v, count);
counts_to_offsets_array_u32(count, result);
return result;
}
internal U64 *
offsets_from_counts_array_u64(Arena *arena, U64 *v, U64 count)
{
U64 *result = push_array_copy_u64(arena, v, count);
counts_to_offsets_array_u64(count, result);
return result;
}
+63
View File
@@ -0,0 +1,63 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct U32Array
{
U64 count;
U32 *v;
} U32Array;
typedef struct U64Array
{
U64 count;
U64 *v;
} U64Array;
typedef struct
{
U64 count;
U128 *v;
} U128Array;
typedef struct U64Node
{
struct U64Node *next;
U64 data;
} U64Node;
typedef struct U64List
{
U64 count;
U64Node *first;
U64Node *last;
} U64List;
typedef struct VoidNode
{
struct VoidNode *next;
void *v;
} VoidNode;
////////////////////////////////
internal U64Node * u64_list_push(Arena *arena, U64List *list, U64 data);
internal void u64_list_concat_in_place(U64List *list, U64List *to_concat);
internal U64Array u64_array_from_list(Arena *arena, U64List *list);
internal U64Array u64_array_remove_duplicates(Arena *arena, U64Array in);
internal void u32_array_sort(U64 count, U32 *v);
internal B32 u32_array_compare(U32Array a, U32Array b);
internal U64 sum_array_u64(U64 count, U64 *v);
internal U64 max_array_u64(U64 count, U64 *v);
internal U64 min_array_u64(U64 count, U64 *v);
internal void counts_to_offsets_array_u32(U64 count, U32 *arr);
internal void counts_to_offsets_array_u64(U64 count, U64 *arr);
internal U32 * offsets_from_counts_array_u32(Arena *arena, U32 *v, U64 count);
internal U64 * offsets_from_counts_array_u64(Arena *arena, U64 *v, U64 count);
+276
View File
@@ -0,0 +1,276 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U32Array
bit_array_init32(Arena *arena, U64 word_count)
{
U32Array result;
result.count = CeilIntegerDiv(word_count, 32);
result.v = push_array(arena, U32, word_count);
return result;
}
internal U64
bit_array_scan_left_to_right32(U32Array bit_array, U64 lo, U64 hi, B32 state)
{
Assert(lo < bit_array.count*32);
Assert(hi <= bit_array.count*32);
Assert(lo <= hi);
Assert(state == 0 || state == 1);
U64 word_lo = lo / 32;
U64 word_hi = CeilIntegerDiv(hi, 32) - 1;
U64 word_idx = word_lo;
U64 bit_idx = 0;
U64 scan_count = hi - lo;
if (scan_count < 32) {
U64 bit_lo = lo % 32;
U64 bit_hi = hi % 32;
U64 word = bit_array.v[word_idx];
word ^= state - 1;
word &= (1U << bit_hi) - (1U << bit_lo);
if (word) {
bit_idx = ctz32(word);
goto exit;
}
} else {
U32 first_word = bit_array.v[word_idx];
first_word ^= state - 1;
first_word &= ~0u << (lo % 32);
if (first_word) {
bit_idx = ctz32(first_word);
goto exit;
}
for (word_idx += 1; word_idx < word_hi; word_idx += 1) {
U32 word = bit_array.v[word_idx];
word ^= state - 1;
if (word != 0) {
bit_idx = ctz32(word);
goto exit;
}
}
U64 bit_hi = hi - (word_idx * 32);
U32 last_word = bit_array.v[word_idx];
last_word ^= state - 1;
last_word &= (1 << bit_hi) - 1;
if (last_word) {
bit_idx = ctz32(last_word);
goto exit;
}
}
word_idx = 0;
bit_idx = max_U32;
exit:;
U64 result = word_idx * 32 + bit_idx;
return result;
}
internal U64
bit_array_scan_right_to_left32(U32Array bit_array, U64 lo, U64 hi, B32 state)
{
Assert(lo <= hi);
Assert(state == 0 || state == 1);
S64 word_lo = lo / 32;
S64 word_hi = CeilIntegerDiv(hi, 32) - 1;
S64 word_idx = word_hi;
S64 bit_idx = -1;
U64 scan_count = hi - lo;
if (scan_count < 32) {
S64 bit_lo = lo % 32;
S64 bit_hi = bit_lo + scan_count;
U32 word = bit_array.v[word_idx];
for (bit_idx = bit_hi; bit_idx >= bit_lo; bit_idx -= 1) {
U32 bit = ExtractBit(word, bit_idx);
if (bit == state) {
goto exit;
}
}
} else {
U32 last_word = bit_array.v[word_idx];
S64 bit_hi = hi % 32;
for (bit_idx = bit_hi; bit_idx >= 0; bit_idx -= 1) {
U32 bit = ExtractBit(last_word, bit_idx);
if (bit == state) {
goto exit;
}
}
for (word_idx -= 1; word_idx > word_lo; word_idx -= 1) {
U32 word = bit_array.v[word_idx];
for (bit_idx = 32 - 1; bit_idx >= 0; bit_idx -= 1) {
U32 bit = ExtractBit(word, bit_idx);
if (bit == state) {
goto exit;
}
}
}
U32 first_word = bit_array.v[word_idx];
S64 bit_lo = lo % 32;
for (bit_idx = 32 - 1; bit_idx >= bit_lo; bit_idx -= 1) {
U32 bit = ExtractBit(first_word, bit_idx);
if (bit == state) {
goto exit;
}
}
}
word_idx = 0;
bit_idx = max_U32;
exit:;
S64 result_s64 = word_idx * 32 + bit_idx;
U64 result_u64 = (U64)result_s64;
return result_u64;
}
internal Rng1U64
bit_array_scan_left_to_right32_contiguous(U32Array bit_array, U64 lo, U64 hi, B32 state, U64 in_row_count)
{
Rng1U64 result = rng_1u64(max_U64, max_U64);
U64 curr_count = 0, rover = lo;
while (curr_count < in_row_count) {
rover = bit_array_scan_left_to_right32(bit_array, rover, hi, state);
// no more bits in range
if (rover >= hi) {
break;
}
// set first match
if (result.v[0] == max_U64) {
result = rng_1u64(rover, rover);
continue;
}
// reset on non-contiguous range
B32 is_bit_index_not_adjoined = (result.v[0] + 1 < rover);
if (is_bit_index_not_adjoined) {
curr_count = 0;
result = rng_1u64(max_U64, max_U64);
continue;
}
// advance
result.v[1] = rover;
curr_count -= 1;
}
// did we allocate enough bits?
if (curr_count != in_row_count) {
result = rng_1u64(max_U64, max_U64);
}
return result;
}
internal Rng1U64
bit_array_scan_right_to_left32_contiguous(U32Array bit_array, U64 lo, U64 hi, B32 state, U64 in_row_count)
{
Rng1U64 result = rng_1u64(max_U64, max_U64);
U64 curr_count = 0, rover = lo;
while (curr_count < in_row_count) {
rover = bit_array_scan_right_to_left32(bit_array, lo, rover, state);
// no more bits in range
if (rover >= hi) {
break;
}
// set first match
if (result.v[0] == max_U64) {
result = rng_1u64(rover, rover);
continue;
}
// reset on non-contiguous range
B32 is_bit_index_not_adjoined = (result.v[0] + 1 < rover);
if (is_bit_index_not_adjoined) {
curr_count = 0;
result = rng_1u64(max_U64, max_U64);
continue;
}
// advance
result.v[0] = rover;
curr_count -= 1;
}
// did we allocate enough bits?
if (curr_count != in_row_count) {
result = rng_1u64(max_U64, max_U64);
}
return result;
}
internal U64
bit_array_find_next_unset_bit32(U32Array bit_array)
{
U64 result = bit_array_scan_left_to_right32(bit_array, 0, bit_array.count*32, 0);
return result;
}
internal U64
bit_array_find_next_set_bit32(U32Array bit_array)
{
U64 result = bit_array_scan_left_to_right32(bit_array, 0, bit_array.count*32, 1);
return result;
}
internal void
bit_array_set_bit32(U32Array bit_array, U64 idx, B32 state)
{
Assert(idx < bit_array.count*32);
U64 word_idx = idx / 32;
U64 bit_idx = idx % 32;
if (state) {
bit_array.v[word_idx] |= (1 << bit_idx);
} else {
bit_array.v[word_idx] &= ~(1 << bit_idx);
}
}
internal void
bit_array_set_bit_range32(U32Array bit_array, Rng1U64 range, B32 state)
{
for (U64 idx = range.min ; idx < range.max; idx += 1) {
bit_array_set_bit32(bit_array, idx, state);
}
}
internal U32
bit_array_get_bit32(U32Array bit_array, U64 idx)
{
Assert(idx < bit_array.count*32);
U64 word_idx = idx / 32;
U64 bit_idx = idx % 32;
U32 bit = (bit_array.v[word_idx] & (1 << bit_idx)) >> bit_idx;
return bit;
}
internal B32
bit_array_is_bit_set(U32Array bit_arr, U64 bit_pos)
{
U64 word_idx = bit_pos / 32;
Assert(word_idx < bit_arr.count);
U32 word = bit_arr.v[word_idx];
U64 bit_idx = bit_pos % 32;
B32 is_set = !!(word & (1 << bit_idx));
return is_set;
}
+18
View File
@@ -0,0 +1,18 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
internal U32Array bit_array_init32(Arena *arena, U64 word_count);
internal U64 bit_array_scan_left_to_right32(U32Array bit_array, U64 lo, U64 hi, B32 state);
internal U64 bit_array_scan_right_to_left32(U32Array bit_array, U64 lo, U64 hi, B32 state);
internal Rng1U64 bit_array_scan_left_to_right32_contiguous(U32Array bit_array, U64 lo, U64 hi, B32 state, U64 in_row_count);
internal Rng1U64 bit_array_scan_right_to_left32_contiguous(U32Array bit_array, U64 lo, U64 hi, B32 state, U64 in_row_count);
internal B32 byte_scan_right_to_left(U8 *start, U8 *opl, U8 byte, U64 *offset_out);
internal U64 bit_array_find_next_unset_bit32(U32Array bit_array);
internal U64 bit_array_find_next_set_bit32(U32Array bit_array);
internal void bit_array_set_bit32(U32Array bit_array, U64 idx, B32 state);
internal void bit_array_set_bit_range32(U32Array bit_array, Rng1U64 range, B32 state);
internal U32 bit_array_get_bit32(U32Array bit_array, U64 idx);
+102
View File
@@ -0,0 +1,102 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmacro-redefined"
#elif defined(_MSC_VER)
#pragma warning (push, 0)
#endif
#include "../third_party_ext/blake3/c/blake3_portable.c"
#if defined(_M_AMD64) || defined(__x86_64__)
#define round_fn sse2_round_fn
#define compress_pre sse2_compress_pre
#include "../third_party_ext/blake3/c/blake3_sse2.c"
#define loadu sse41_loadu
#define storeu sse41_storeu
#define addv sse41_addv
#define xorv sse41_xorv
#define set1 sse41_set1
#define set4 sse41_set4
#define rot16 sse41_rot16
#define rot12 sse41_rot12
#define rot8 sse41_rot8
#define rot7 sse41_rot7
#define g1 sse41_g1
#define g2 sse41_g2
#define diagonalize sse41_diagonalize
#define undiagonalize sse41_undiagonalize
#define compress_pre sse41_compress_pre
#define round_fn sse41_round_fn
#define transpose_vecs sse41_transpose_vecs
#define transpose_msg_vecs sse41_transpose_msg_vecs
#define load_counters sse41_load_counters
#if defined(__clang__)
#pragma clang attribute push(__attribute__((target("sse4.1"))), apply_to=function)
#endif
#include "../third_party_ext/blake3/c/blake3_sse41.c"
#if defined(__clang__)
#pragma clang attribute pop
#endif
#define loadu avx2_loadu
#define storeu avx2_storeu
#define addv avx2_addv
#define xorv avx2_xorv
#define set1 avx2_set1
#define rot7 avx2_rot7
#define rot8 avx2_rot8
#define rot12 avx2_rot12
#define rot16 avx2_rot16
#define round_fn avx2_round_fn
#define transpose_vecs avx2_transpose_vecs
#define transpose_msg_vecs avx2_transpose_msg_vecs
#define load_counters avx2_load_counters
#if defined(__clang__)
#pragma clang attribute push(__attribute__((target("avx2"))), apply_to=function)
#endif
#include "../third_party_ext/blake3/c/blake3_avx2.c"
#if defined(__clang__)
#pragma clang attribute pop
#endif
#define set4 avx512_set4
#define g1 avx512_g1
#define g2 avx512_g2
#define diagonalize avx512_diagonalize
#define undiagonalize avx512_undiagonalize
#define compress_pre avx512_compress_pre
#define transpose_vecs avx512_transpose_vecs
#define transpose_msg_vecs avx512_transpose_msg_vecs
#define load_counters avx512_load_counters
#if defined(__clang__)
#pragma clang attribute push(__attribute__((target("avx512f,avx512vl"))), apply_to=function)
#endif
#include "../third_party_ext/blake3/c/blake3_avx512.c"
#if defined(__clang__)
#pragma clang attribute pop
#endif
#endif
#if defined(__aarch64__) || defined(_M_ARM64)
#include "../third_party_ext/blake3/c/blake3_neon.c"
#endif
#include "../third_party_ext/blake3/c/blake3_dispatch.c"
#include "../third_party_ext/blake3/c/blake3.c"
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(_MSC_VER)
#pragma warning (pop, 0)
#endif
+41
View File
@@ -0,0 +1,41 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#if defined(__clang__) && defined(__x86_64__)
# if defined(__IMMINTRIN_H)
# error "include this header before immintrin.h / x86intrin.h / intrin.h"
# endif
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wreserved-macro-identifier"
# pragma push_macro("__AVX__")
# pragma push_macro("__AVX2__")
# pragma push_macro("__SSE4_1__")
# pragma push_macro("__AVX512F__")
# pragma push_macro("__AVX512VL__")
# define __AVX__ 1
# define __AVX2__ 1
# define __SSE4_1__ 1
# define __AVX512F__ 1
# define __AVX512VL__ 1
# include <immintrin.h>
# pragma pop_macro("__AVX512VL__")
# pragma pop_macro("__AVX512F__")
# pragma pop_macro("__SSE4_1__")
# pragma pop_macro("__AVX2__")
# pragma pop_macro("__AVX__")
# pragma clang diagnostic pop
#endif
#include "../third_party_ext/blake3/c/blake3.h"
static void
blake3(void* out, size_t outlen, void* in, size_t inlen)
{
blake3_hasher hasher;
blake3_hasher_init(&hasher);
blake3_hasher_update(&hasher, in, inlen);
blake3_hasher_finalize(&hasher, (uint8_t*)out, outlen);
}
+13
View File
@@ -0,0 +1,13 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#include "../third_party_ext/blake3/blake3_portable.c"
#if defined(__aarch64__) || defined(_M_ARM64)
#include "../third_party_ext/blake3/blake3_neon.c"
#endif
#include "../third_party_ext/blake3/blake3_dispatch.c"
#include "../third_party_ext/blake3/blake3.c"
#pragma comment (lib, "blake3")
+18
View File
@@ -0,0 +1,18 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef BASE_BLAKE3_H
#define BASE_BLAKE3_H
#include "../third_party_ext/blake3/blake3.h"
static void
blake3(void* out, size_t outlen, void* in, size_t inlen)
{
blake3_hasher hasher;
blake3_hasher_init(&hasher);
blake3_hasher_update(&hasher, in, inlen);
blake3_hasher_finalize(&hasher, (uint8_t*)out, outlen);
}
#endif // BASE_BLAKE3_H
+227
View File
@@ -0,0 +1,227 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U16
safe_cast_u16x(U64 x)
{
AssertAlways(x <= max_U16);
return (U16)x;
}
////////////////////////////////
internal U64
u128_mod64(U128 a, U64 b)
{
return a.u64[1] % b;
}
////////////////////////////////
internal Version
make_version(U64 major, U64 minor)
{
Version version;
version.major = major;
version.minor = minor;
return version;
}
internal int
version_compar(Version a, Version b)
{
int cmp = 0;
if (a.major < b.major) {
cmp = -1;
} else if (a.major > b.major) {
cmp = +1;
} else if (a.major == b.major) {
if (a.minor < b.minor) {
cmp = -1;
} else if (a.minor > b.minor) {
cmp = +1;
}
}
return cmp;
}
////////////////////////////////
internal ISectOff
isect_off(U32 isect, U32 off)
{
ISectOff result = { isect, off };
return result;
}
////////////////////////////////
internal int
u16_compar(const void *raw_a, const void *raw_b)
{
U16 a = *(U16*)raw_a;
U16 b = *(U16*)raw_b;
int result = a < b ? -1 :
a > b ? +1 :
0;
return result;
}
internal int
u32_compar(const void *raw_a, const void *raw_b)
{
U32 a = *(U32*)raw_a;
U32 b = *(U32*)raw_b;
int result = a < b ? -1 :
a > b ? +1 :
0;
return result;
}
internal int
u64_compar(const void *raw_a, const void *raw_b)
{
U64 a = *(const U64*)raw_a;
U64 b = *(const U64*)raw_b;
int result = a < b ? -1 : a > b ? +1 : 0;
return result;
}
internal int
u64_compar_inv(const void *raw_a, const void *raw_b)
{
U64 a = *(const U64*)raw_a;
U64 b = *(const U64*)raw_b;
int result = a < b ? +1 : a > b ? -1 : 0;
return result;
}
internal int
u16_compar_is_before(void *raw_a, void *raw_b)
{
U16 *a = (U16 *)raw_a;
U16 *b = (U16 *)raw_b;
int is_before = *a < *b;
return is_before;
}
internal int
u32_compar_is_before(void *raw_a, void *raw_b)
{
U32 *a = (U32 *)raw_a;
U32 *b = (U32 *)raw_b;
int is_before = *a < *b;
return is_before;
}
internal int
u64_compar_is_before(void *raw_a, void *raw_b)
{
U64 *a = (U64 *)raw_a;
U64 *b = (U64 *)raw_b;
int is_before = *a < *b;
return is_before;
}
internal int
u8_is_before(void *raw_a, void *raw_b)
{
U8 *a = (U8 *) raw_a;
U8 *b = (U8 *) raw_b;
return *a < *b;
}
internal int
u16_is_before(void *raw_a, void *raw_b)
{
U16 *a = (U16 *) raw_a;
U16 *b = (U16 *) raw_b;
return *a < *b;
}
internal int
u32_is_before(void *raw_a, void *raw_b)
{
U32 *a = (U32 *) raw_a;
U32 *b = (U32 *) raw_b;
return *a < *b;
}
internal int
u64_is_before(void *raw_a, void *raw_b)
{
U64 *a = (U64 *) raw_a;
U64 *b = (U64 *) raw_b;
return *a < *b;
}
internal int
pair_u32_is_before_v0(void *raw_a, void *raw_b)
{
PairU32 *a = raw_a;
PairU32 *b = raw_b;
return a->v0 < b->v0;
}
internal int
pair_u32_is_before(void *raw_a, void *raw_b)
{
PairU32 *a = raw_a;
PairU32 *b = raw_b;
return a->v1 < b->v1;
}
internal int
pair_u64_is_before_v0(void *raw_a, void *raw_b)
{
PairU64 *a = raw_a;
PairU64 *b = raw_b;
return a->v0 < b->v0;
}
internal int
pair_u64_is_before_v1(void *raw_a, void *raw_b)
{
PairU64 *a = raw_a;
PairU64 *b = raw_b;
return a->v1 < b->v1;
}
internal int
pair_u32_compar_v0(const void *raw_a, const void *raw_b)
{
const PairU32 *a = raw_a;
const PairU32 *b = raw_b;
return u32_compar(&a->v0, &b->v0);
}
internal int
pair_u64_compar_v0(const void *raw_a, const void *raw_b)
{
const PairU64 *a = raw_a;
const PairU64 *b = raw_b;
return u64_compar(&a->v0, &b->v0);
}
internal int
pair_u64_compar_v1(const void *raw_a, const void *raw_b)
{
const PairU64 *a = raw_a;
const PairU64 *b = raw_b;
return u64_compar(&a->v1, &b->v1);
}
////////////////////////////////
internal void
str8_list_concat_in_place_array(String8List *list, String8List *arr, U64 count)
{
for (U64 i = 0; i < count; ++i) {
str8_list_concat_in_place(list, &arr[i]);
}
}
+199
View File
@@ -0,0 +1,199 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#if COMPILER_MSVC
# define COMPILER_STRING "MSVC"
#elif COMPILER_CLANG
# define COMPILER_STRING "Clang"
#elif COMPILER_GCC
# define COMPILER_STRING "GCC"
#else
# error "undefined compiler string"
#endif
#if BUILD_DEBUG
# define BUILD_MODE_STRING "Debug"
#else
# define BUILD_MODE_STRING "Release"
#endif
////////////////////////////////
#define BitExtract(x, count, shift) (((x) >> (shift)) & ((1 << (count)) - 1))
////////////////////////////////
#if OS_WINDOWS
# define ins_atomic_ptr_eval_cond_assign(x,k,c) InterlockedCompareExchangePointer((volatile PVOID *)(x),(k),(c))
# define ins_atomic_u32_add_eval(x,c) InterlockedAdd((volatile LONG *)(x), c)
# define ins_atomic_u32_inc_eval(x) InterlockedIncrement((volatile LONG *)x)
#else
# error "atomics are not defined for this system"
#endif
////////////////////////////////
// Linked List Helpers
#define DLLConcatInPlace(list, to_concat) do { \
if ((to_concat)->count) { \
if ((list)->count) { \
(list)->last->next = (to_concat)->first; \
(to_concat)->first->prev = (list)->last; \
(list)->last = (to_concat)->last; \
} else { \
(list)->first = (to_concat)->first; \
(list)->last = (to_concat)->last; \
} \
(list)->count += (to_concat)->count; \
MemoryZeroStruct(to_concat); \
} \
} while (0)
#define DLLConcatInPlaceArray(list, to_concat_arr, count) for (U64 i = 0; i < (count); i += 1) { DLLConcatInPlace(list, &(to_concat_arr)[i]); }
#define SLLQueuePushCount(list, node) do { \
SLLQueuePush((list)->first, (list)->last, node); \
++(list)->count; \
} while (0)
#define SLLConcatInPlaceNoCount(list, to_concat) do { \
if ((to_concat)->first) { \
if ((list)->first) { \
(list)->last->next = (to_concat)->first; \
(list)->last = (to_concat)->last; \
} else { \
(list)->first = (to_concat)->first; \
(list)->last = (to_concat)->last; \
} \
MemoryZeroStruct(to_concat); \
} \
} while (0)
#define SLLConcatInPlace(list, to_concat) do { \
if ((to_concat)->count) { \
if ((list)->count) { \
(list)->last->next = (to_concat)->first; \
(list)->last = (to_concat)->last; \
} else { \
(list)->first = (to_concat)->first; \
(list)->last = (to_concat)->last; \
} \
(list)->count += (to_concat)->count; \
MemoryZeroStruct(to_concat); \
} \
} while (0)
#define SLLConcatInPlaceArray(list, to_concat_arr, count) for (U64 i = 0; i < (count); ++i) { SLLConcatInPlace(list, &(to_concat_arr)[i]); }
#define SLLConcatInPlaceChunkList(list, to_concat, chunk_type) do { \
if ((list)->last != 0) { \
U64 base_cursor = (list)->last->base + (list)->last->count; \
for (chunk_type *c = (to_concat)->first; c != 0; c = c->next) { \
c->base = base_cursor; \
base_cursor += c->count; \
} \
} \
SLLConcatInPlace(list, to_concat); \
} while (0)
#define SLLConcatInPlaceChunkListArray(list, to_concat_arr, type, count) for (U64 i = 0; i < (count); ++i) { SLLConcatInPlaceChunkList(list, &(to_concat_arr)[i], type); }
#define SLLChunkListPush(_arena, _list, _cap, _value_type) do { \
if ((_list)->last == 0 || (_list)->last->count >= (_list)->last->cap) { \
_value_type##Chunk *new_chunk = push_array(_arena, _value_type##Chunk, 1); \
new_chunk->v = push_array(_arena, _value_type, _cap); \
new_chunk->cap = _cap; \
new_chunk->base = (_list)->last ? (_list)->last->base + (_list)->last->cap : 0; \
SLLQueuePushCount(_list, new_chunk); \
} \
_value_type *v = &(_list)->last->v[(_list)->last->count++]; \
v->chunk = (_list)->last; \
} while (0)
#define SLLChunkListPushZero(_arena, _list, _cap, _value_type) do { \
SLLChunkListPush(_arena, _list, _cap, _value_type); \
MemoryZeroStruct(SLLChunkListLastItem(_list)); \
SLLChunkListLastItem(_list)->chunk = (_list)->last; \
} while(0)
#define SLLChunkListLastItem(_list) (&(_list)->last->v[(_list)->last->count - 1])
////////////////////////////////
#define MemoryIsZeroStruct(p) memory_is_zero(p, sizeof(*p))
////////////////////////////////
#if ARCH_LITTLE_ENDIAN
# define BE_U32(x) bswap_u32(x)
#else
# define BE_U32(x) (x)
#endif
////////////////////////////////
typedef struct
{
U64 major;
U64 minor;
} Version;
////////////////////////////////
typedef struct ISectOff
{
U32 isect;
U32 off;
} ISectOff;
////////////////////////////////
typedef struct PairU32
{
U32 v0;
U32 v1;
} PairU32;
typedef struct PairU64
{
U64 v0;
U64 v1;
} PairU64;
////////////////////////////////
internal U16 safe_cast_u16x(U64 x);
////////////////////////////////
internal U64 u128_mod64(U128 a, U64 b);
////////////////////////////////
internal Version make_version(U64 major, U64 minor);
internal int version_compar(Version a, Version b);
////////////////////////////////
internal ISectOff isect_off(U32 isect, U32 off);
////////////////////////////////
internal int u16_compar(const void *raw_a, const void *raw_b);
internal int u32_compar(const void *raw_a, const void *raw_b);
internal int u64_compar(const void *raw_a, const void *raw_b);
internal int u8_is_before(void *raw_a, void *raw_b);
internal int u16_is_before(void *raw_a, void *raw_b);
internal int u32_is_before(void *raw_a, void *raw_b);
internal int u64_is_before(void *raw_a, void *raw_b);
internal int pair_u32_is_before_v0(void *raw_a, void *raw_b);
internal int pair_u32_is_before_v1(void *raw_a, void *raw_b);
internal int pair_u64_is_before_v0(void *raw_a, void *raw_b);
internal int pair_u64_is_before_v1(void *raw_a, void *raw_b);
////////////////////////////////
internal void str8_list_concat_in_place_array(String8List *list, String8List *arr, U64 count);
+72
View File
@@ -0,0 +1,72 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U32
update_crc32(U32 crc, U8 *ptr, U64 size)
{
// CRC-32 algo borrowed from stb.h
local_persist U32 crc_table[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535,
0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd,
0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d,
0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac,
0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab,
0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb,
0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea,
0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce,
0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409,
0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739,
0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268,
0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0,
0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8,
0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703,
0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7,
0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae,
0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6,
0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d,
0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5,
0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
#if 0
for (U32 i = 0; i < 256; ++i) {
U32 s = i;
for (U32 j = 0; j < 8; ++j) {
s = (s >> 1) ^ (s & 1 ? 0xedb88320 : 0);
}
crc_table[i] = s;
}
#endif
crc = ~crc;
for (U32 i = 0; i < size; ++i) {
crc = (crc >> 8) ^ crc_table[(ptr[i] ^ crc) & 0xff];
}
return ~crc;
}
internal U32
crc32_from_string(String8 string)
{
return update_crc32(0, string.str, string.size);
}
+8
View File
@@ -0,0 +1,8 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
internal U32 update_crc32(U32 crc, U8 *ptr, U64 size);
internal U32 crc32_from_string(String8 string);
+12
View File
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#include "base_core.c"
#include "base_strings.c"
#include "base_arena.c"
#include "base_math.c"
#include "base_arrays.c"
#include "base_bit_array.c"
#include "base_crc32.c"
#include "base_md5.c"
+15
View File
@@ -0,0 +1,15 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#include "base_core.h"
#include "base_strings.h"
#include "base_arena.h"
#include "base_math.h"
#include "base_arrays.h"
#include "base_blake3.h"
#include "base_bit_array.h"
#include "base_crc32.h"
#include "base_md5.h"
+19
View File
@@ -0,0 +1,19 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal void
rng_1u64_list_push_node(Rng1U64List *list, Rng1U64Node *node)
{
SLLQueuePush(list->first, list->last, node);
++list->count;
}
internal Rng1U64Node *
rng_1u64_list_push(Arena *arena, Rng1U64List *list, Rng1U64 range)
{
Rng1U64Node *node = push_array(arena, Rng1U64Node, 1);
node->v = range;
rng_1u64_list_push_node(list, node);
return node;
}
+23
View File
@@ -0,0 +1,23 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct Rng1U64Node
{
struct Rng1U64Node *next;
Rng1U64 v;
} Rng1U64Node;
typedef struct Rng1U64List
{
U64 count;
Rng1U64Node *first;
Rng1U64Node *last;
} Rng1U64List;
////////////////////////////////
internal void rng_1u64_list_push_node(Rng1U64List *list, Rng1U64Node *node);
internal Rng1U64Node * rng_1u64_list_push(Arena *arena, Rng1U64List *list, Rng1U64 range);
+12
View File
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal MD5Hash
md5_hash_from_string(String8 data)
{
MD5_CTX ctx; MD5_Init(&ctx);
MD5_Update(&ctx, (void*)data.str, safe_cast_u32(data.size));
MD5Hash hash; MD5_Final((unsigned char*)&hash, &ctx);
return hash;
}
+12
View File
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct MD5Hash
{
U8 value[16];
} MD5Hash;
internal MD5Hash md5_hash_from_string(String8 data);
+248
View File
@@ -0,0 +1,248 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
global read_only String8 g_null_string;
internal String8
str8_cstring_capped_reverse(void *start, void *cap)
{
char *ptr = cap;
while (ptr > (char *)start) {
--ptr;
if (*ptr == '\0') break;
}
U64 null_offset = (U64)(ptr - (char *) start);
String8 result = str8((U8 *) start, null_offset);
return result;
}
internal U64
str8_find_needle_reverse(String8 string, U64 start_pos, String8 needle, StringMatchFlags flags)
{
for (S64 i = string.size - start_pos - needle.size; i >= 0; --i) {
String8 haystack = str8_substr(string, rng_1u64(i, i + needle.size));
if (str8_match(haystack, needle, flags)) {
return (U64)i + needle.size;
}
}
return 0;
}
internal int
str8_compar(String8 a, String8 b, B32 ignore_case)
{
int cmp = 0;
U64 size = Min(a.size, b.size);
if (ignore_case) {
for (U64 i = 0; i < size; ++i) {
U8 la = char_to_lower(a.str[i]);
U8 lb = char_to_lower(b.str[i]);
if (la < lb) {
cmp = -1;
break;
} else if (la > lb) {
cmp = +1;
break;
}
}
} else {
for (U64 i = 0; i < size; ++i) {
if (a.str[i] < b.str[i]) {
cmp = -1;
break;
} else if (a.str[i] > b.str[i]) {
cmp = +1;
break;
}
}
}
if (cmp == 0) {
// shorter prefix must precede longer prefixes
if (a.size > b.size) {
cmp = +1;
} else if (b.size > a.size) {
cmp = -1;
}
}
return cmp;
}
internal int
str8_compar_ignore_case(const void *a, const void *b)
{
return str8_compar(*(String8*)a, *(String8*)b, 1);
}
internal int
str8_compar_case_sensetive(const void *a, const void *b)
{
return str8_compar(*(String8*)a, *(String8*)b, 0);
}
internal int
str8_is_before_case_sensetive(const void *a, const void *b)
{
int cmp = str8_compar_case_sensetive(a, b);
return cmp < 0;
}
internal String8Node *
str8_list_push_raw(Arena *arena, String8List *list, void *data_ptr, U64 data_size)
{
String8 data = str8((U8 *)data_ptr, data_size);
String8Node *node = str8_list_push(arena, list, data);
return node;
}
internal U64
str8_list_push_pad(Arena *arena, String8List *list, U64 offset, U64 align)
{
U64 pad_size = AlignPow2(offset, align) - offset;
U8 *pad = push_array(arena, U8, pad_size);
MemorySet(pad, 0, pad_size);
str8_list_push(arena, list, str8(pad, pad_size));
return pad_size;
}
internal U64
str8_list_push_pad_front(Arena *arena, String8List *list, U64 offset, U64 align)
{
U64 pad_size = AlignPow2(offset, align) - offset;
U8 *pad = push_array(arena, U8, pad_size);
MemorySet(pad, 0, pad_size);
str8_list_push_front(arena, list, str8(pad, pad_size));
return pad_size;
}
internal String8List
str8_list_arr_concat(String8List *v, U64 count)
{
String8List result = {0};
for (U64 i = 0; i < count; i += 1) {
str8_list_concat_in_place(&result, &v[i]);
}
return result;
}
internal String8Node *
str8_list_push_many(Arena *arena, String8List *list, U64 count)
{
String8Node *arr = push_array(arena, String8Node, count);
for (U64 i = 0; i < count; ++i) {
str8_list_push_node(list, arr + i);
}
return arr;
}
internal String8
str8_from_bits_u32(Arena *arena, U32 x)
{
U8 c0 = 'a' + ((x >> 28) & 0xf);
U8 c1 = 'a' + ((x >> 24) & 0xf);
U8 c2 = 'a' + ((x >> 20) & 0xf);
U8 c3 = 'a' + ((x >> 16) & 0xf);
U8 c4 = 'a' + ((x >> 12) & 0xf);
U8 c5 = 'a' + ((x >> 8) & 0xf);
U8 c6 = 'a' + ((x >> 4) & 0xf);
U8 c7 = 'a' + ((x >> 0) & 0xf);
String8 result = push_str8f(arena, "%c%c%c%c%c%c%c%c", c0, c1, c2, c3, c4, c5, c6, c7);
return result;
}
internal String8
str8_from_bits_u64(Arena *arena, U64 x)
{
U8 c0 = 'a' + ((x >> 60) & 0xf);
U8 c1 = 'a' + ((x >> 56) & 0xf);
U8 c2 = 'a' + ((x >> 52) & 0xf);
U8 c3 = 'a' + ((x >> 48) & 0xf);
U8 c4 = 'a' + ((x >> 44) & 0xf);
U8 c5 = 'a' + ((x >> 40) & 0xf);
U8 c6 = 'a' + ((x >> 36) & 0xf);
U8 c7 = 'a' + ((x >> 32) & 0xf);
U8 c8 = 'a' + ((x >> 28) & 0xf);
U8 c9 = 'a' + ((x >> 24) & 0xf);
U8 ca = 'a' + ((x >> 20) & 0xf);
U8 cb = 'a' + ((x >> 16) & 0xf);
U8 cc = 'a' + ((x >> 12) & 0xf);
U8 cd = 'a' + ((x >> 8) & 0xf);
U8 ce = 'a' + ((x >> 4) & 0xf);
U8 cf = 'a' + ((x >> 0) & 0xf);
String8 result = push_str8f(arena,
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf);
return result;
}
internal String8Node *
str8_list_pop_front(String8List *list)
{
String8Node *node = 0;
if (list->node_count) {
node = list->first;
Assert(list->total_size >= list->first->string.size);
list->node_count -= 1;
list->total_size -= list->first->string.size;
SLLQueuePop(list->first, list->last);
}
return node;
}
internal String8
str8_from_memory_size2(Arena *arena, U64 size)
{
String8 result;
if (size < KB(1)) {
result = push_str8f(arena, "%llu Bytes", size);
} else if (size < MB(1)) {
result = push_str8f(arena, "%llu.%02llu KiB", size / KB(1), ((size * 100) / KB(1)) % 100);
} else if (size < GB(1)) {
result = push_str8f(arena, "%llu.%02llu MiB", size / MB(1), ((size * 100) / MB(1)) % 100);
} else if (size < TB(1)) {
result = push_str8f(arena, "%llu.%02llu GiB", size / GB(1), ((size * 100) / GB(1)) % 100);
} else {
result = push_str8f(arena, "%llu.%02llu TiB", size / TB(1), ((size * 100) / TB(1)) % 100);
}
return result;
}
internal String8
str8_from_count(Arena *arena, U64 count)
{
String8 result;
if (count < 1000) {
result = push_str8f(arena, "%llu", count);
} else if (count < 1000000) {
U64 frac = ((count * 100) / 1000) % 100;
if (frac) {
result = push_str8f(arena, "%llu.%02lluK", count / 1000, frac);
} else {
result = push_str8f(arena, "%lluK", count / 1000);
}
} else if (count < 1000000000) {
U64 frac = ((count * 100) / 1000000) % 100;
if (frac) {
result = push_str8f(arena, "%llu.%02lluM", count / 1000000, frac);
} else {
result = push_str8f(arena, "%lluM", count / 1000000);
}
} else {
U64 frac = ((count * 100) * 1000000000) % 100;
if (frac) {
result = push_str8f(arena, "%llu.%02lluB", count / 1000000000, frac);
} else {
result = push_str8f(arena, "%lluB", count / 1000000000, frac);
}
}
return result;
}
internal U64
hash_from_str8(String8 string)
{
XXH64_hash_t hash64 = XXH3_64bits(string.str, string.size);
return hash64;
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#define MemoryCopyStr8(dst, s) MemoryCopy(dst, (s).str, (s).size)
internal String8 str8_cstring_capped_reverse(void *start, void *cap);
internal U64 str8_find_needle_reverse(String8 string, U64 start_pos, String8 needle, StringMatchFlags flags);
internal int str8_compar(String8 a, String8 b, B32 ignore_case);
internal int str8_compar_ignore_case(const void *a, const void *b);
internal int str8_compar_case_sensetive(const void *a, const void *b);
#define str8_list_push_struct(a,l,d) str8_list_push_raw(a, l, d, sizeof(*d))
internal String8Node * str8_list_push_raw(Arena *arena, String8List *list, void *data_ptr, U64 data_size);
internal U64 str8_list_push_pad(Arena *arena, String8List *list, U64 offset, U64 align);
internal U64 str8_list_push_pad_front(Arena *arena, String8List *list, U64 offset, U64 align);
internal String8List str8_list_arr_concat(String8List *v, U64 count);
internal String8Node * str8_list_push_many(Arena *arena, String8List *list, U64 count);
internal String8 str8_from_bits_u32(Arena *arena, U32 x);
internal String8 str8_from_bits_u64(Arena *arena, U64 x);
// TODO: remove
internal String8Node * str8_list_pop_front(String8List *list);
internal String8 str8_from_memory_size2(Arena *arena, U64 size);
internal String8 str8_from_count(Arena *arena, U64 count);
internal U64 hash_from_str8(String8 string);
File diff suppressed because it is too large Load Diff
+584
View File
@@ -0,0 +1,584 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#define CV_MinComplexTypeIndex 0x1000
////////////////////////////////
// Aligns
#define CV_LeafAlign 4
#define CV_SymbolAlign 1
#define CV_C13SubSectionAlign 4
#define CV_FileCheckSumsAlign 4
////////////////////////////////
//- Symbol and Leaf Headers
#define CV_LeafSize_Max max_U16
typedef U16 CV_LeafSize;
#define CV_SymSize_Max max_U16
typedef U16 CV_SymSize;
typedef struct CV_LeafHeader
{
CV_LeafSize size;
CV_LeafKind kind;
} CV_LeafHeader;
typedef struct CV_SymbolHeader
{
CV_SymSize size;
CV_SymKind kind;
} CV_SymbolHeader;
////////////////////////////////
// Type Index Helpers
typedef enum CV_TypeIndexSource
{
CV_TypeIndexSource_NULL,
CV_TypeIndexSource_TPI,
CV_TypeIndexSource_IPI,
CV_TypeIndexSource_COUNT
} CV_TypeIndexSource;
typedef struct CV_TypeIndexInfo
{
struct CV_TypeIndexInfo *next;
U64 offset;
CV_TypeIndexSource source;
} CV_TypeIndexInfo;
typedef struct CV_TypeIndexInfoList
{
U64 count;
CV_TypeIndexInfo *first;
CV_TypeIndexInfo *last;
} CV_TypeIndexInfoList;
typedef struct CV_TypeIndexArray
{
U32 count;
CV_TypeIndex *v;
} CV_TypeIndexArray;
//- $$Symbols
typedef struct CV_Symbol
{
CV_SymKind kind;
U64 offset;
String8 data;
} CV_Symbol;
typedef struct CV_SymbolNode
{
struct CV_SymbolNode *next;
struct CV_SymbolNode *prev;
CV_Symbol data;
} CV_SymbolNode;
typedef struct CV_SymbolPtrNode
{
struct CV_SymbolPtrNode *next;
CV_Symbol *data;
} CV_SymbolPtrNode;
typedef struct CV_SymbolList
{
U64 count;
CV_Signature signature;
CV_SymbolNode *first;
CV_SymbolNode *last;
} CV_SymbolList;
typedef struct CV_SymbolListArray
{
U64 count;
CV_SymbolList *v;
} CV_SymbolListArray;
typedef struct CV_SymbolPtrArray
{
U64 count;
CV_SymbolNode **v;
} CV_SymbolPtrArray;
typedef struct CV_Scope
{
struct CV_ScopeList *children;
struct CV_Scope *next;
struct CV_Scope *prev;
CV_Symbol symbol;
} CV_Scope;
typedef struct CV_ScopeList
{
CV_Scope *first;
CV_Scope *last;
} CV_ScopeList;
typedef struct CV_ScopeFrame
{
struct CV_ScopeFrame *next;
CV_ScopeList *list;
CV_Scope *curr;
U64 symbol_off;
U32 *parent_off_ptr;
U32 *end_off_ptr;
} CV_ScopeFrame;
//- $$FileChksms
typedef struct CV_Checksum
{
CV_C13Checksum *header;
String8 value;
} CV_Checksum;
typedef struct CV_ChecksumNode
{
struct CV_ChecksumNode *next;
CV_Checksum data;
} CV_ChecksumNode;
typedef struct CV_ChecksumList
{
U64 count;
CV_ChecksumNode *first;
CV_ChecksumNode *last;
} CV_ChecksumList;
//- $$Lines
typedef struct CV_LineArray
{
U32 file_off;
U64 line_count;
U64 col_count;
U64 *voffs; // [line_count + 1]
U32 *line_nums; // [line_count]
U16 *col_nums; // [line_count * 2]
} CV_LineArray;
typedef struct CV_File
{
U32 file_off;
CV_LineArray lines;
} CV_File;
typedef struct CV_C13LinesHeader
{
U64 sec_idx;
U64 sec_off_lo;
U64 sec_off_hi;
U64 file_off;
U64 line_count;
U64 col_count;
U64 line_array_off;
U64 col_array_off;
} CV_C13LinesHeader;
typedef struct CV_C13LinesHeaderNode
{
struct CV_C13LinesHeaderNode *next;
CV_C13LinesHeader v;
} CV_C13LinesHeaderNode;
typedef struct CV_C13LinesHeaderList
{
CV_C13LinesHeaderNode *first;
CV_C13LinesHeaderNode *last;
U64 count;
} CV_C13LinesHeaderList;
////////////////////////////////
typedef struct CV_UDTInfo
{
String8 name;
String8 unique_name;
CV_TypeProps props;
} CV_UDTInfo;
typedef struct CV_TypeServerInfo
{
String8 name;
OS_Guid sig;
U32 age;
} CV_TypeServerInfo;
typedef struct CV_TypeServerInfoNode
{
struct CV_TypeServerInfoNode *next;
CV_TypeServerInfo data;
} CV_TypeServerInfoNode;
typedef struct CV_TypeServerInfoList
{
CV_TypeServerInfoNode *first;
CV_TypeServerInfoNode *last;
U64 count;
} CV_TypeServerInfoList;
typedef struct CV_PrecompInfo
{
CV_TypeIndex start_index;
U32 sig;
U32 leaf_count;
String8 obj_name;
} CV_PrecompInfo;
typedef struct CV_ObjInfo
{
U32 sig;
String8 name;
} CV_ObjInfo;
////////////////////////////////
// Accels
typedef struct CV_Line
{
U64 voff;
U32 file_off;
U32 line_num;
U16 col_num;
} CV_Line;
typedef struct CV_LinesAccel
{
U64 map_count;
CV_Line *map;
} CV_LinesAccel;
typedef struct CV_InlineeLinesAccel
{
U64 bucket_count;
U64 bucket_max;
CV_C13InlineeLinesParsed **buckets;
} CV_InlineeLinesAccel;
typedef struct CV_InlineBinaryAnnotsParsed
{
U64 lines_count;
CV_LineArray *lines;
Rng1U64List code_ranges;
} CV_InlineBinaryAnnotsParsed;
typedef struct CV_C13InlineeLinesParsedList
{
CV_C13InlineeLinesParsedNode *first;
CV_C13InlineeLinesParsedNode *last;
U64 count;
} CV_C13InlineeLinesParsedList;
////////////////////////////////
typedef U32 CV_C13SubSectionIdxKind;
enum
{
CV_C13SubSectionIdxKind_NULL,
#define X(N,c) CV_C13SubSectionIdxKind_##N,
CV_C13SubSectionKindXList(X)
#undef X
CV_C13SubSectionIdxKind_COUNT
};
typedef struct CV_C13SubSectionList
{
CV_C13SubSectionNode *first;
CV_C13SubSectionNode *last;
U64 count;
} CV_C13SubSectionList;
////////////////////////////////
typedef struct CV_DebugS
{
String8List data_list[CV_C13SubSectionIdxKind_COUNT];
} CV_DebugS;
typedef struct CV_DebugT
{
U64 size;
U64 count;
U8 **v;
} CV_DebugT;
////////////////////////////////
//~ Leaf Helpers
typedef struct CV_Leaf
{
CV_LeafKind kind;
String8 data;
} CV_Leaf;
typedef struct CV_LeafNode
{
struct CV_LeafNode *next;
CV_Leaf data;
} CV_LeafNode;
typedef struct CV_LeafList
{
U64 count;
CV_LeafNode *first;
CV_LeafNode *last;
} CV_LeafList;
////////////////////////////////
//~ String Hash Table
typedef struct CV_StringTableRange
{
struct CV_StringTableRange *next;
Rng1U64 range;
U64 debug_s_idx;
} CV_StringTableRange;
typedef struct CV_StringBucket
{
String8 string;
union {
struct {
U32 idx0;
U32 idx1;
};
U64 offset;
} u;
} CV_StringBucket;
typedef struct CV_StringHashTable
{
U64 total_string_size;
U64 total_insert_count;
U64 bucket_cap;
CV_StringBucket **buckets;
} CV_StringHashTable;
typedef struct CV_StringHashTableResult
{
U64 string_count;
CV_StringBucket **buckets;
} CV_StringHashTableResult;
////////////////////////////////
//~ Task Contexts
typedef struct
{
U64 cap;
union {
CV_SymbolNode ***buckets;
CV_SymbolNode **deref_buckets;
} u;
Rng1U64 *ranges;
CV_SymbolNode **symbols;
} CV_SymbolDeduperTask;
typedef struct
{
CV_SymbolList *list_arr;
Rng1U64 *list_range_arr;
U64 *symbol_base_arr;
CV_SymbolNode **symbol_arr;
} CV_SymbolListSyncer;
typedef struct
{
CV_DebugS *arr;
CV_StringTableRange **range_lists;
U64 *string_counts;
U64 bucket_cap;
CV_StringBucket **buckets;
U64 total_string_size;
U64 total_insert_count;
} CV_DedupStringTablesTask;
typedef struct
{
U8 *buffer;
Rng1U64 *ranges;
CV_StringBucket **buckets;
} CV_PackStringHashTableTask;
typedef struct
{
CV_DebugT debug_t;
Rng1U64 *ranges;
String8List *lists;
String8Node *nodes;
} CV_Str8ListFromDebugT;
////////////////////////////////
// Type Index Helpers
internal CV_TypeIndexInfo * cv_symbol_type_index_info_push(Arena *arena, CV_TypeIndexInfoList *list, CV_TypeIndexSource source, U64 offset);
internal CV_TypeIndexInfoList cv_get_symbol_type_index_offsets(Arena *arena, CV_SymKind kind, String8 data);
internal CV_TypeIndexInfoList cv_get_leaf_type_index_offsets(Arena *arena, CV_LeafKind leaf_kind, String8 data);
internal CV_TypeIndexInfoList cv_get_inlinee_type_index_offsets(Arena *arena, String8 raw_data);
internal String8Array cv_get_data_around_type_indices(Arena *arena, CV_TypeIndexInfoList ti_list, String8 data);
internal CV_TypeIndexSource cv_type_index_source_from_leaf_kind(CV_LeafKind leaf_kind);
////////////////////////////////
internal U64 cv_name_offset_from_symbol(CV_SymKind kind, String8 data);
internal String8 cv_name_from_symbol (CV_SymKind kind, String8 data);
internal String8 cv_name_from_udt_info (CV_UDTInfo udt_info);
internal B32 cv_is_udt_name_anon (String8 name);
internal B32 cv_is_udt (CV_LeafKind kind);
internal B32 cv_is_global_symbol (CV_SymKind kind);
internal B32 cv_is_typedef (CV_SymKind kind);
internal B32 cv_is_scope_symbol (CV_SymKind kind);
internal B32 cv_is_end_symbol (CV_SymKind kind);
internal B32 cv_is_leaf_type_server(CV_LeafKind kind);
internal B32 cv_is_leaf_pch (CV_LeafKind kind);
internal CV_ObjInfo cv_obj_info_from_symbol(CV_Symbol symbol);
internal CV_TypeServerInfo cv_type_server_info_from_leaf(CV_Leaf leaf);
internal CV_PrecompInfo cv_precomp_info_from_leaf(CV_Leaf leaf);
internal B32 cv_is_reg_sp(CV_Arch arch, CV_Reg reg);
////////////////////////////////
//~ Leaf Helpers
internal U64 cv_compute_leaf_record_size(String8 data, U64 align);
internal U64 cv_serialize_leaf_to_buffer(U8 *buffer, U64 buffer_cursor, U64 buffer_size, CV_LeafKind kind, String8 data, U64 align);
internal String8 cv_serialize_leaf_ex(Arena *arena, CV_LeafKind kind, String8 data, U64 align);
internal String8 cv_serialize_leaf(Arena *arena, CV_Leaf *leaf, U64 align);
internal CV_Leaf cv_make_leaf(Arena *arena, CV_LeafKind kind, String8 data);
internal U64 cv_deserial_leaf(String8 raw_data, U64 off, U64 align, CV_Leaf *leaf_out);
internal CV_Leaf cv_leaf_from_string(String8 raw_data);
////////////////////////////////
//~ Symbol Helpers
internal U64 cv_compute_symbol_record_size(CV_Symbol *symbol, U64 align);
internal U64 cv_serialize_symbol_to_buffer(U8 *buffer, U64 buffer_cursor, U64 buffer_size, CV_Symbol *symbol, U64 align);
internal String8 cv_serialize_symbol(Arena *arena, CV_Symbol *symbol, U64 align);
internal String8 cv_make_symbol(Arena *arena, CV_SymKind kind, String8 data);
internal String8 cv_make_obj_name(Arena *arena, String8 obj_path, U32 sig);
internal String8 cv_make_comp3(Arena *arena,
CV_Compile3Flags flags, CV_Language lang, CV_Arch arch,
U16 ver_fe_major, U16 ver_fe_minor, U16 ver_fe_build, U16 ver_feqfe,
U16 ver_major, U16 ver_minor, U16 ver_build, U16 ver_qfe,
String8 version_string);
internal String8 cv_make_envblock(Arena *arena, String8List string_list);
internal CV_Symbol cv_make_proc_ref(Arena *arena, CV_ModIndex imod, U32 stream_offset, String8 name, B32 is_local);
internal CV_Symbol cv_make_pub32(Arena *arena, CV_Pub32Flags flags, U32 off, U16 isect, String8 name);
internal CV_SymbolList cv_make_proc_refs(Arena *arena, CV_ModIndex imod, CV_SymbolList symbol_list);
////////////////////////////////
// .debug$S Helpers
internal CV_DebugS cv_parse_debug_s_c13(Arena *arena, String8 raw_debug_s);
internal CV_DebugS cv_parse_debug_s_c13_list(Arena *arena, String8List raw_debug_s);
internal CV_DebugS cv_parse_debug_s(Arena *arena, String8 raw_debug_s);
internal void cv_debug_s_concat_in_place(CV_DebugS *dst, CV_DebugS *src);
internal String8List cv_data_c13_from_debug_s(Arena *arena, CV_DebugS *debug_s, B32 write_sig);
internal CV_C13SubSectionIdxKind cv_c13_sub_section_idx_from_kind(CV_C13SubSectionKind kind);
internal String8List * cv_sub_section_ptr_from_debug_s(CV_DebugS *debug_s, CV_C13SubSectionKind kind);
internal String8List cv_sub_section_from_debug_s(CV_DebugS debug_s, CV_C13SubSectionKind kind);
internal String8 cv_string_table_from_debug_s(CV_DebugS debug_s);
internal String8 cv_file_chksms_from_debug_s(CV_DebugS debug_s);
////////////////////////////////
//~ .debug$T helpers
internal CV_DebugT cv_debug_t_from_data_arr(Arena *arena, String8Array data_arr, U64 align);
internal CV_DebugT cv_debug_t_from_data(Arena *arena, String8 data, U64 align);
internal CV_Leaf cv_debug_t_get_leaf(CV_DebugT debug_t, U64 leaf_idx);
internal String8 cv_debug_t_get_raw_leaf(CV_DebugT debug_t, U64 leaf_idx);
internal CV_LeafHeader * cv_debug_t_get_leaf_header(CV_DebugT debug_t, U64 leaf_idx);
internal B32 cv_debug_t_is_pch(CV_DebugT debug_t);
internal B32 cv_debug_t_is_type_server(CV_DebugT debug_t);
internal U64 cv_debug_t_array_count_leaves(U64 count, CV_DebugT *arr);
internal String8List cv_str8_list_from_debug_t_parallel(TP_Context *tp, Arena *arena, CV_DebugT types);
// $$Symbols
internal void cv_parse_symbol_sub_section(Arena *arena, CV_SymbolList *list, U64 offset_base, String8 data, U64 align);
internal void cv_symbol_list_push_node(CV_SymbolList *list, CV_SymbolNode *node);
internal CV_SymbolNode * cv_symbol_list_push(Arena *arena, CV_SymbolList *list);
internal CV_SymbolNode * cv_symbol_list_push_data(Arena *arena, CV_SymbolList *list, CV_SymKind kind, String8 data);
internal CV_SymbolNode * cv_symbol_list_push_many(Arena *arena, CV_SymbolList *list, U64 count);
internal void cv_symbol_list_remove_node(CV_SymbolList *list, CV_SymbolNode *node);
internal void cv_symbol_list_concat_in_place(CV_SymbolList *list, CV_SymbolList *to_concat);
internal void cv_symbol_list_concat_in_place_arr(CV_SymbolList *list, U64 count, CV_SymbolList *to_concat);
internal U64 cv_symbol_list_arr_get_count(U64 count, CV_SymbolList *list_arr);
internal String8List cv_data_from_symbol_list(Arena *arena, CV_SymbolList symbol_list, U64 align);
internal CV_SymbolList cv_global_scope_symbols_from_list(Arena *arena, CV_SymbolList list);
internal CV_ScopeList cv_symbol_tree_from_symbol_list(Arena *arena, CV_SymbolList list);
internal CV_SymbolList cv_build_symbol_tree(Arena *arena, CV_ScopeList symbol_tree, U64 symbol_base, U64 align);
internal CV_SymbolPtrArray cv_symbol_ptr_array_from_list(Arena *arena, TP_Context *tp, U64 count, CV_SymbolList *symbol_list_arr);
// $$FileChksms
#define CV_MAP_STRING_TO_OFFSET_FUNC(name) U64 name(void *ud, String8 string)
typedef CV_MAP_STRING_TO_OFFSET_FUNC(CV_MapStringToOffsetFunc);
//internal String8 cv_c13_file_chksms_from_sub_sections(String8 c13_data, CV_C13Parsed *ss);
internal void cv_c13_patch_string_offsets_in_checksum_list(CV_ChecksumList checksum_list, String8 string_data, U64 string_data_base_offset, CV_StringHashTable string_ht);
internal String8List cv_c13_collect_source_file_names(Arena *arena, CV_ChecksumList checksum_list, String8 string_data);
// $$Lines
internal CV_C13LinesHeaderList cv_c13_lines_from_sub_sections(Arena *arena, String8 c13_data, Rng1U64 ss_range);
internal CV_LineArray cv_c13_line_array_from_data(Arena *arena, String8 c13_data, U64 sec_base, CV_C13LinesHeader parsed_lines);
// $$InlineeLines
internal CV_C13InlineeLinesParsedList cv_c13_inlinee_lines_from_sub_sections(Arena *arena, String8List raw_inlinee_lines);
internal CV_InlineBinaryAnnotsParsed cv_c13_parse_inline_binary_annots(Arena *arena, U64 parent_voff, CV_C13InlineeLinesParsed *inlinee_parsed, String8 binary_annots);
// $$FrameData
internal void cv_c13_patch_checksum_offsets_in_frame_data_list(String8List frame_data, U32 checksum_rebase);
////////////////////////////////
// $$Lines Accel
internal void cv_make_c13_files(Arena *arena, String8 c13_data, CV_C13SubSectionList lines, U64 *file_count_out, CV_C13File **files_out);
internal CV_LinesAccel * cv_make_lines_accel(Arena *arena, U64 lines_count, CV_LineArray *lines);
internal CV_Line * cv_line_from_voff(CV_LinesAccel *accel, U64 voff, U64 *out_line_count);
////////////////////////////////
// $$InlineeLines Accel
internal U64 cv_c13_inlinee_lines_accel_hash(void *buffer, U64 size);
internal B32 cv_c13_inlinee_lines_accel_push(CV_InlineeLinesAccel *accel, CV_C13InlineeLinesParsed *parsed);
internal CV_C13InlineeLinesParsed * cv_c13_inlinee_lines_accel_find(CV_InlineeLinesAccel *accel, CV_ItemId inlinee);
internal CV_InlineeLinesAccel * cv_c13_make_inlinee_lines_accel(Arena *arena, CV_C13InlineeLinesParsedList sub_sects);
////////////////////////////////
// String Hash Table
internal U64 cv_string_hash_table_hash(String8 string);
internal CV_StringHashTable cv_dedup_string_tables(TP_Arena *arena, TP_Context *tp, U64 count, CV_DebugS *arr);
internal CV_StringHashTableResult cv_serialize_string_hash_table(Arena *arena, TP_Context *tp, CV_StringHashTable string_ht);
internal String8 cv_pack_string_hash_table(Arena *arena, TP_Context *tp, CV_StringHashTable string_ht);
////////////////////////////////
internal CV_EncodedFramePtrReg cv_pick_fp_encoding(CV_SymFrameproc *frameproc, B32 is_local_param);
internal CV_Reg cv_decode_fp_reg(CV_Arch arch, CV_EncodedFramePtrReg encoded_reg);
internal Rng1U64List cv_make_defined_range_list_from_gaps(Arena *arena, Rng1U64 defrange, CV_LvarAddrGap *gaps, U64 gap_count);
////////////////////////////////
internal U64 cv_size_from_reg_x86(CV_Reg reg);
internal U64 cv_size_from_reg_x64(CV_Reg reg);
internal U64 cv_size_from_reg(CV_Arch arch, CV_Reg reg);
////////////////////////////////
internal CV_Arch cv_arch_from_coff_machine(COFF_MachineType machine);
internal String8 cv_string_from_type_index_source(CV_TypeIndexSource ti_source);
+270
View File
@@ -0,0 +1,270 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal void
bucket_list_concat_in_place(BucketList *list, BucketList *to_concat)
{
SLLConcatInPlaceNoCount(list, to_concat);
}
internal BucketNode *
bucket_list_pop(BucketList *list)
{
BucketNode *result = list->first;
SLLQueuePop(list->first, list->last);
return result;
}
////////////////////////////////
internal U64
hash_table_hasher(String8 string)
{
XXH64_hash_t hash64 = XXH3_64bits(string.str, string.size);
return hash64;
}
internal HashTable *
hash_table_init(Arena *arena, U64 cap)
{
HashTable *ht = push_array(arena, HashTable, 1);
ht->cap = cap;
ht->buckets = push_array(arena, BucketList, cap);
return ht;
}
internal void
hash_table_purge(HashTable *ht)
{
// reset key count
ht->count = 0;
// concat buckets
for (U64 ibucket = 0; ibucket < ht->cap; ++ibucket) {
bucket_list_concat_in_place(&ht->free_buckets, &ht->buckets[ibucket]);
}
}
internal BucketNode *
hash_table_push(Arena *arena, HashTable *ht, U64 hash, KeyValuePair v)
{
BucketNode *node;
if (ht->free_buckets.first != 0) {
node = bucket_list_pop(&ht->free_buckets);
} else {
node = push_array(arena, BucketNode, 1);
}
node->next = 0;
node->v = v;
U64 ibucket = hash % ht->cap;
SLLQueuePush(ht->buckets[ibucket].first, ht->buckets[ibucket].last, node);
++ht->count;
return node;
}
internal BucketNode *
hash_table_push_string_string(Arena *arena, HashTable *ht, String8 key, String8 value)
{
U64 hash = hash_table_hasher(key);
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_string = key, .value_string = value });
}
internal BucketNode *
hash_table_push_string_raw(Arena *arena, HashTable *ht, String8 key, void *value)
{
U64 hash = hash_table_hasher(key);
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_string = key, .value_raw = value });
}
internal BucketNode *
hash_table_push_string_u64(Arena *arena, HashTable *ht, String8 key, U64 value)
{
U64 hash = hash_table_hasher(key);
return hash_table_push(arena, ht, hash, (KeyValuePair){.key_string = key, .value_u64 = value });
}
internal BucketNode *
hash_table_push_u32_raw(Arena *arena, HashTable *ht, U32 key, void *value)
{
U64 hash = hash_table_hasher(str8_struct(&key));
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_u32 = key, .value_raw = value });
}
internal BucketNode *
hash_table_push_u32_string(Arena *arena, HashTable *ht, U32 key, String8 value)
{
U64 hash = hash_table_hasher(str8_struct(&key));
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_u32 = key, .value_string = value });
}
internal BucketNode *
hash_table_push_u64_raw(Arena *arena, HashTable *ht, U64 key, void *value)
{
U64 hash = hash_table_hasher(str8_struct(&key));
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_u64 = key, .value_raw = value });
}
internal BucketNode *
hash_table_push_u64_string(Arena *arena, HashTable *ht, U64 key, String8 value)
{
U64 hash = hash_table_hasher(str8_struct(&key));
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_u64 = key, .value_string = value });
}
internal BucketNode *
hash_table_push_u64_u64(Arena *arena, HashTable *ht, U64 key, U64 value)
{
U64 hash = hash_table_hasher(str8_struct(&key));
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_u64 = key, .value_u64 = value });
}
internal BucketNode *
hash_table_push_path_string(Arena *arena, HashTable *ht, String8 path, String8 value)
{
String8 path_canon = path_canon_from_regular_path(arena, path);
return hash_table_push_string_string(arena, ht, path_canon, value);
}
internal BucketNode *
hash_table_push_path_u64(Arena *arena, HashTable *ht, String8 path, U64 value)
{
String8 path_canon = path_canon_from_regular_path(arena, path);
U64 hash = hash_table_hasher(path_canon);
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_string = path_canon, .value_u64 = value });
}
internal BucketNode *
hash_table_push_path_raw(Arena *arena, HashTable *ht, String8 path, void *value)
{
String8 path_canon = path_canon_from_regular_path(arena, path);
U64 hash = hash_table_hasher(path_canon);
return hash_table_push(arena, ht, hash, (KeyValuePair){ .key_string = path_canon, .value_raw = value });
}
////////////////////////////////
internal KeyValuePair *
hash_table_search_string(HashTable *ht, String8 key_string)
{
U64 hash = hash_table_hasher(key_string);
U64 ibucket = hash % ht->cap;
BucketList *bucket = ht->buckets + ibucket;
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
if (str8_match(n->v.key_string, key_string, 0)) {
return &n->v;
}
}
return 0;
}
internal KeyValuePair *
hash_table_search_u32(HashTable *ht, U32 key_u32)
{
U64 hash = hash_table_hasher(str8_struct(&key_u32));
U64 ibucket = hash % ht->cap;
BucketList *bucket = ht->buckets + ibucket;
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
if (n->v.key_u32 == key_u32) {
return &n->v;
}
}
return 0;
}
internal KeyValuePair *
hash_table_search_u64(HashTable *ht, U64 key_u64)
{
U64 hash = hash_table_hasher(str8_struct(&key_u64));
U64 ibucket = hash % ht->cap;
BucketList *bucket = ht->buckets + ibucket;
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
if (n->v.key_u64 == key_u64) {
return &n->v;
}
}
return 0;
}
internal KeyValuePair *
hash_table_search_path(HashTable *ht, String8 path)
{
Temp scratch = scratch_begin(0,0);
String8 path_canon = path;
path_canon = lower_from_str8(scratch.arena, path_canon);
path_canon = path_convert_slashes(scratch.arena, path_canon, PathStyle_UnixAbsolute);
KeyValuePair *result = hash_table_search_string(ht, path_canon);
scratch_end(scratch);
return result;
}
internal B32
hash_table_search_path_u64(HashTable *ht, String8 key, U64 *value_out)
{
KeyValuePair *result = hash_table_search_path(ht, key);
if (result != 0) {
if (value_out != 0) {
*value_out = result->value_u64;
}
return 1;
}
return 0;
}
internal B32
hash_table_search_string_u64(HashTable *ht, String8 key, U64 *value_out)
{
KeyValuePair *result = hash_table_search_string(ht, key);
if (result != 0) {
if (value_out != 0) {
*value_out = result->value_u64;
}
return 1;
}
return 0;
}
////////////////////////////////
internal int
key_value_pair_is_before_u32(void *raw_a, void *raw_b)
{
KeyValuePair *a = raw_a;
KeyValuePair *b = raw_b;
return a->key_u32 < b->key_u32;
}
internal int
key_value_pair_is_before_u64(void *raw_a, void *raw_b)
{
KeyValuePair *a = raw_a;
KeyValuePair *b = raw_b;
return a->key_u64 < b->key_u64;
}
internal KeyValuePair *
key_value_pairs_from_hash_table(Arena *arena, HashTable *ht)
{
KeyValuePair *pairs = push_array_no_zero(arena, KeyValuePair, ht->count);
for (U64 bucket_idx = 0, cursor = 0; bucket_idx < ht->cap; ++bucket_idx) {
for (BucketNode *n = ht->buckets[bucket_idx].first; n != 0; n = n->next) {
Assert(cursor < ht->count);
pairs[cursor++] = n->v;
}
}
return pairs;
}
internal void
sort_key_value_pairs_as_u32(KeyValuePair *pairs, U64 count)
{
radsort(pairs, count, key_value_pair_is_before_u32);
}
internal void
sort_key_value_pairs_as_u64(KeyValuePair *pairs, U64 count)
{
radsort(pairs, count, key_value_pair_is_before_u64);
}
+81
View File
@@ -0,0 +1,81 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct KeyValuePair
{
union {
String8 key_string;
U32 key_u32;
U64 key_u64;
};
union {
String8 value_string;
void *value_raw;
U32 value_u32;
U64 value_u64;
};
} KeyValuePair;
typedef struct BucketNode
{
struct BucketNode *next;
KeyValuePair v;
} BucketNode;
typedef struct BucketList
{
BucketNode *first;
BucketNode *last;
} BucketList;
typedef struct HashTable
{
U64 count;
U64 cap;
BucketList *buckets;
BucketList free_buckets;
} HashTable;
////////////////////////////////
//- bucket list helpers
internal void bucket_list_concat_in_place(BucketList *list, BucketList *to_concat);
internal BucketNode * bucket_list_pop(BucketList *list);
//- main
internal U64 hash_table_hasher(String8 string);
internal HashTable * hash_table_init(Arena *arena, U64 cap);
internal void hash_table_purge(HashTable *ht);
//- push
internal BucketNode * hash_table_push (Arena *arena, HashTable *ht, U64 hash, KeyValuePair v);
internal BucketNode * hash_table_push_u32_string (Arena *arena, HashTable *ht, U32 key, String8 value);
internal BucketNode * hash_table_push_u64_string (Arena *arena, HashTable *ht, U64 key, String8 value);
internal BucketNode * hash_table_push_string_string(Arena *arena, HashTable *ht, String8 key, String8 value);
internal BucketNode * hash_table_push_path_string (Arena *arena, HashTable *ht, String8 key, String8 value);
internal BucketNode * hash_table_push_u32_raw (Arena *arena, HashTable *ht, U32 key, void *value);
internal BucketNode * hash_table_push_u64_raw (Arena *arena, HashTable *ht, U64 key, void *value);
internal BucketNode * hash_table_push_path_raw (Arena *arena, HashTable *ht, String8 path, void *value);
internal BucketNode * hash_table_push_path_u64 (Arena *arena, HashTable *ht, String8 path, U64 value);
internal BucketNode * hash_table_push_u64_u64 (Arena *arena, HashTable *ht, U64 key, U64 value);
//- search
internal KeyValuePair * hash_table_search_string (HashTable *ht, String8 string);
internal KeyValuePair * hash_table_search_u32 (HashTable *ht, U32 key);
internal KeyValuePair * hash_table_search_u64 (HashTable *ht, U64 key);
internal KeyValuePair * hash_table_search_path (HashTable *ht, String8 path);
internal B32 hash_table_search_path_u64(HashTable *ht, String8 key, U64 *value_out);
//- key-value helpers
internal KeyValuePair * key_value_pairs_from_hash_table(Arena *arena, HashTable *ht);
internal void sort_key_value_pairs_as_u32(KeyValuePair *pairs, U64 count);
internal void sort_key_value_pairs_as_u64(KeyValuePair *pairs, U64 count);
+4591
View File
File diff suppressed because it is too large Load Diff
+298
View File
@@ -0,0 +1,298 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#define LNK_NULL_SYMBOL_NAME "NULL"
#define LNK_TEXT_SYMBOL_NAME "TEXT"
#define LNK_DATA_SYMBOL_NAME "DATA"
#define LNK_RDATA_SYMBOL_NAME "RDATA"
#define LNK_BSS_SYMBOL_NAME "BSS"
#define LNK_XDATA_SYMBOL_NAME "XDATA"
#define LNK_PDATA_SYMBOL_NAME "PDATA"
#define LNK_BASE_RELOC_SYMBOL_NAME "BASE_RELOC"
#define LNK_EDATA_SYMBOL_NAME "EDATA"
#define LNK_DEBUG_DIR_SYMBOL_NAME "DEDIR"
#define LNK_DEBUG_DATA_SYMBOL_NAME "DEDAT"
#define LNK_CV_DIR_SYMBOL_NAME "CV_DIR"
#define LNK_CV_HEADER_PDB70_SYMBOL_NAME "CV_HEADER_PDB70"
#define LNK_CV_HEADER_RDI_SYMBOL_NAME "CV_HEADER_RDI"
#define LNK_CV_HEADER_GUID_SYMBOL_NAME "CV_HEADER_GUID"
#define LNK_RSRC_SYMBOL_NAME "RSRC"
#define LNK_DEBUG_SYMBOL_NAME "DEBUG"
#define LNK_GFIDS_SYMBOL_NAME "GFIDS"
#define LNK_GIATS_SYMBOL_NAME "GIATS"
#define LNK_GLJMP_SYMBOL_NAME "GLJMP"
#define LNK_GEHCONT_SYMBOL_NAME "GEHCONT"
#define LNK_IMPORT_NAME_TABLE_SYMBOL_NAME "IMPORT_STR"
#define LNK_IMPORT_DLL_TABLE_SYMBOL_NAME "IDATA"
#define LNK_IMPORT_ILT_SYMBOL_NAME "ILT"
#define LNK_IMPORT_IAT_SYMBOL_NAME "IAT"
#define LNK_IMPORT_JMP_SYMBOL_NAME "IMPORT_THUNKS"
#define LNK_DELAYED_IMPORT_DLL_TABLE_SYMBOL_NAME "DELAYED_IMPORT_DLL_TABLE"
#define LNK_DELAYED_IMPORT_HANDLE_TABLE_SYMBOL_NAME "DELAYED_IMPORT_HANDLE_TABLE"
#define LNK_DELAYED_IMPORT_INT_SYMBOL_NAME "DELAYED_IMPORT_INT"
#define LNK_DELAYED_IMPORT_IAT_SYMBOL_NAME "DELAYED_IMPORT_IAT"
#define LNK_DELAYED_IMPORT_ILT_SYMBOL_NAME "DELAYED_IMPORT_ILT"
#define LNK_DELAYED_IMPORT_BIAT_SYMBOL_NAME "DELAYED_IMPORT_BIAT"
#define LNK_DELAYED_IMPORT_UIAT_SYMBOL_NAME "DELAYED_IMPORT_UIAT"
#define LNK_DELAYED_IMPORT_CODE_SYMBOL_NAME "DELAYED_IMPORT_CODE"
#define LNK_WIN32_HEADER_SYMBOL_NAME "WIN32_HEADER"
#define LNK_DOS_SYMBOL_NAME "DOS"
#define LNK_NT_HEADERS_SYMBOL_NAME "NT_HEADERS"
#define LNK_PE_MAGIC_CONTAINER_SYMBOL_NAME "PE_MAGIC_CONTAINER"
#define LNK_COFF_FILE_HEADER_CONTAINER_SYMBOL_NAME "COFF_FILE_HEADER_CONTAINER"
#define LNK_PE_OPT_HEADER_CONTAINER_SYMBOL_NAME "PE_OPTIONAL_HEADER_CONTAINER"
#define LNK_COFF_SECTION_HEADER_CONTAINER_SYMBOL_NAME "COFF_SECTION_HEADER_CONTAINER"
#define LNK_DOS_HEADER_SYMBOL_NAME "DOS_HEADER"
#define LNK_DOS_PROGRAM_SYMBOL_NAME "DOS_PROGRAM"
#define LNK_PE_MAGIC_SYMBOL_NAME "PE_MAGIC"
#define LNK_COFF_HEADER_SYMBOL_NAME "COFF_HEADER"
#define LNK_PE_DIRECTORY_ARRAY_SYMBOL_NAME "PE_DIRECTORY_ARRAY"
#define LNK_PE_DIRECTORY_COUNT_SYMBOL_NAME "PE_DIRECTORY_COUNT"
#define LNK_PE_OPT_HEADER_SYMBOL_NAME "PE_OPTIONAL_HEADER"
#define LNK_COFF_SECT_HEADER_ARRAY_SYMBOL_NAME "COFF_SECT_HEADER_ARRAY"
#define LNK_COFF_SECT_HEADER_COUNT_SYMBOL_NAME "COFF_SECT_HEADER_COUNT"
#define LNK_PE_CHECKSUM_SYMBOL_NAME "PE_CHECKSUM"
// _tls_used is a special section in CRT which has format of
// PE_TLSHeader32 or PE_TLSHeader64, according to machine type.
#define LNK_TLS_SYMBOL_NAME "_tls_used"
// _load_config_used points to SYMS_PeLoadConfig32/SYMS_PeLoadConfig64
// and symbols below are used to patch patricual fields of the struct.
#define LNK_LOAD_CONFIG_SYMBOL_NAME "_load_config_used"
#define LNK_ENCLAVE_CONFIG_SYMBOL_NAME "__enclave_config"
#define LNK_GUARD_FLAGS_SYMBOL_NAME "__guard_flags"
#define LNK_GUARD_FIDS_TABLE_SYMBOL_NAME "__guard_fids_table"
#define LNK_GUARD_FIDS_COUNT_SYMBOL_NAME "__guard_fids_count"
#define LNK_GUARD_IAT_TABLE_SYMBOL_NAME "__guard_iat_table"
#define LNK_GUARD_IAT_COUNT_SYMBOL_NAME "__guard_iat_count"
#define LNK_GUARD_LONGJMP_TABLE_SYMBOL_NAME "__guard_longjmp_table"
#define LNK_GUARD_LONGJMP_COUNT_SYMBOL_NAME "__guard_longjmp_count"
#define LNK_GUARD_EHCONT_TABLE_SYMBOL_NAME "__guard_eh_cont_table"
#define LNK_GUARD_EHCONT_COUNT_SYMBOL_NAME "__guard_eh_cont_count"
// x86 load config fields
#define LNK_SAFE_SE_HANDLER_TABLE_SYMBOL_NAME "__safe_se_handler_table"
#define LNK_SAFE_SE_HANDLER_COUNT_SYMBOL_NAME "__safe_se_handler_count"
// load symbols from delayimp.lib
#define LNK_DELAY_LOAD_HELPER2_SYMBOL_NAME "__delayLoadHelper2"
#define LNK_DELAY_LOAD_HELPER2_X86_SYMBOL_NAME "___delayLoadHelper2@8"
#define LNK_TEXT_SECTION_FLAGS (COFF_SectionFlag_CNT_CODE|COFF_SectionFlag_MEM_EXECUTE|COFF_SectionFlag_MEM_READ)
#define LNK_DATA_SECTION_FLAGS (COFF_SectionFlag_CNT_INITIALIZED_DATA|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE)
#define LNK_RDATA_SECTION_FLAGS (COFF_SectionFlag_CNT_INITIALIZED_DATA|COFF_SectionFlag_MEM_READ)
#define LNK_BSS_SECTION_FLAGS (COFF_SectionFlag_CNT_UNINITIALIZED_DATA|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE)
#define LNK_IDATA_SECTION_FLAGS LNK_DATA_SECTION_FLAGS
#define LNK_DEBUG_DIR_SECTION_FLAGS LNK_DATA_SECTION_FLAGS
#define LNK_RSRC_SECTION_FLAGS LNK_DATA_SECTION_FLAGS
#define LNK_XDATA_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_PDATA_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_EDATA_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_GFIDS_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_GIATS_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_GLJMP_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_GEHCONT_SECTION_FLAGS LNK_RDATA_SECTION_FLAGS
#define LNK_RELOC_SECTION_FLAGS (LNK_RDATA_SECTION_FLAGS | COFF_SectionFlag_MEM_DISCARDABLE)
#define LNK_DEBUG_SECTION_FLAGS (LNK_RDATA_SECTION_FLAGS | COFF_SectionFlag_MEM_DISCARDABLE)
////////////////////////////////
typedef enum
{
LNK_InputSource_CmdLine, // specified on command line
LNK_InputSource_Default, // specified through defaultlib switch
LNK_InputSource_Obj, // refrenced from objects
LNK_InputSource_Count
} LNK_InputSourceType;
typedef String8Node LNK_InputLib;
typedef String8List LNK_InputLibList;
typedef struct LNK_InputImport
{
COFF_ImportHeader import_header;
struct LNK_InputImport *next;
} LNK_InputImport;
typedef struct LNK_InputImportList
{
U64 count;
LNK_InputImport *first;
LNK_InputImport *last;
} LNK_InputImportList;
////////////////////////////////
typedef struct LNK_BaseRelocPage
{
U64 voff;
U64List entries;
} LNK_BaseRelocPage;
typedef struct LNK_BaseRelocPageNode
{
struct LNK_BaseRelocPageNode *next;
LNK_BaseRelocPage v;
} LNK_BaseRelocPageNode;
typedef struct LNK_BaseRelocPageList
{
U64 count;
LNK_BaseRelocPageNode *first;
LNK_BaseRelocPageNode *last;
} LNK_BaseRelocPageList;
typedef struct LNK_BaseRelocPageArray
{
U64 count;
LNK_BaseRelocPage *v;
} LNK_BaseRelocPageArray;
typedef struct
{
U64 page_size;
LNK_Section **sect_id_map;
LNK_Reloc **reloc_arr;
Rng1U64 *range_arr;
LNK_BaseRelocPageList *list_arr;
HashTable **page_ht_arr;
} LNK_BaseRelocTask;
typedef struct
{
Rng1U64 *ranges;
U64 page_size;
LNK_Section **sect_id_map;
LNK_BaseRelocPageList *list_arr;
LNK_Obj **obj_arr;
HashTable **page_ht_arr;
} LNK_ObjBaseRelocTask;
typedef struct
{
Rng1U64 *range_arr;
LNK_LibNode *lib_arr;
LNK_Symbol **symbol_arr_arr;
} LNK_LazyIniter;
typedef struct
{
LNK_InputObjList input_obj_list;
LNK_InputImportList input_import_list;
LNK_SymbolList unresolved_symbol_list;
} LNK_SymbolFinderResult;
typedef struct
{
PathStyle path_style;
LNK_SymbolTable *symtab;
LNK_SymbolNodeArray lookup_node_arr;
LNK_SymbolFinderResult *result_arr;
Rng1U64 *range_arr;
} LNK_SymbolFinder;
typedef struct
{
LNK_SymbolTable *symtab;
LNK_SectionTable *st;
LNK_Section **sect_id_map;
U64 base_addr;
LNK_Section **sect_arr;
Rng1U64 *range_arr;
} LNK_SectionRelocPatcher;
typedef struct
{
LNK_SymbolTable *symtab;
LNK_SectionTable *st;
LNK_Section **sect_id_map;
U64 base_addr;
LNK_Obj **obj_arr;
} LNK_ObjRelocPatcher;
typedef struct
{
String8 path;
String8 data;
} LNK_WriteThreadContext;
typedef struct
{
String8 data;
Rng1U64 *ranges;
U128 *hashes;
} LNK_Blake3Hasher;
////////////////////////////////
internal LNK_InputImport * lnk_input_import_list_push(Arena *arena, LNK_InputImportList *list);
internal void lnk_input_import_list_concat_in_place(LNK_InputImportList *list, LNK_InputImportList *to_concat);
internal LNK_InputImport ** lnk_input_import_arr_from_list(Arena *arena, LNK_InputImportList list);
internal LNK_InputImportList lnk_list_from_input_import_arr(LNK_InputImport **arr, U64 count);
////////////////////////////////
// Helpers
internal void lnk_write_data_list_to_file_path(String8 path, String8List list);
internal void lnk_write_data_to_file_path(String8 path, String8 data);
internal String8 lnk_make_full_path(Arena *arena, String8 work_dir, PathStyle system_path_style, String8 path);
internal String8 lnk_get_lib_name(String8 path);
internal B32 lnk_is_lib_disallowed(HashTable *disallow_lib_ht, String8 path);
internal B32 lnk_is_lib_loaded(HashTable *default_lib_ht, HashTable *loaded_lib_ht, LNK_InputSourceType input_source, String8 lib_path);
internal void lnk_push_disallow_lib(Arena *arena, HashTable *disallow_lib_ht, String8 path);
internal void lnk_push_loaded_lib(Arena *arena, HashTable *default_lib_ht, HashTable *loaded_lib_ht, String8 path);
////////////////////////////////
// Manifest
internal String8List lnk_make_linker_manifest(Arena *arena, B32 manifest_uac, String8 manifest_level, String8 manifest_ui_access, String8List manifest_dependency_list);
internal void lnk_merge_manifest_files(Arena *arena, String8 mt_path, String8 manifest_name, String8List manifest_path_list);
internal String8 lnk_res_from_data(Arena *arena, String8 data);
////////////////////////////////
// Resources
internal void lnk_serialize_pe_resource_tree(LNK_SectionTable *st, LNK_SymbolTable *symtab, PE_ResourceDir *root_dir);
internal void lnk_add_resource_debug_s(LNK_SectionTable *st, LNK_SymbolTable *symtab, String8 obj_path, String8 cwd_path, String8 exe_path, CV_Arch arch, String8List res_file_list, MD5Hash *res_hash_array);
internal String8 lnk_make_res_obj(TP_Context *tp, Arena *arena, PE_ResourceDir *root_dir, COFF_MachineType machine, COFF_TimeStamp time_stamp, String8 path, String8 cwd_path, String8 exe_path, String8List res_file_list, MD5Hash *res_hash_array);
internal String8 lnk_obj_from_res_file_list(TP_Context *tp, Arena *arena, LNK_SectionTable *st, LNK_SymbolTable *symtab, String8List res_file_list, String8List res_path_list, COFF_MachineType machine, U32 time_stamp, String8 work_dir, PathStyle system_path_style, String8 obj_name);
////////////////////////////////
// Debug
internal String8 lnk_make_linker_coff_obj(TP_Context *tp, Arena *arena, COFF_TimeStamp time_stamp, COFF_MachineType machine, String8 cwd_path, String8 exe_path, String8 pdb_path, String8 cmd_line, String8 obj_name);
////////////////////////////////
// Win32 Image Helpers
internal void lnk_build_debug_pdb(LNK_SectionTable *st, LNK_SymbolTable *symtab, LNK_Section *debug_sect, LNK_Chunk *debug_dir_array_chunk, COFF_TimeStamp time_stamp, OS_Guid guid, U32 age, String8 pdb_path);
internal void lnk_build_debug_rdi(LNK_SectionTable *st, LNK_SymbolTable *symtab, LNK_Section *debug_sect, LNK_Chunk *debug_dir_array_chunk, COFF_TimeStamp time_stamp, OS_Guid guid, String8 rdi_path);
internal void lnk_build_guard_tables(TP_Context *tp, LNK_SectionTable *st, LNK_SymbolTable *symtab, LNK_ExportTable *exptab, LNK_ObjList obj_list, COFF_MachineType machine, String8 entry_point_name, LNK_GuardFlags guard_flags, B32 emit_suppress_flag);
internal void lnk_build_base_relocs(TP_Context *tp, TP_Arena *tp_arena, LNK_SectionTable *st, LNK_SymbolTable *symtab, COFF_MachineType machine, U64 page_size, LNK_ObjList obj_list);
internal LNK_Chunk * lnk_build_dos_header(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent_chunk);
internal LNK_Chunk * lnk_build_pe_magic(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent);
internal LNK_Chunk * lnk_build_coff_file_header(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent, COFF_MachineType machine, COFF_TimeStamp time_stamp, PE_ImageFileCharacteristics file_characteristics);
internal LNK_Chunk * lnk_build_pe_optional_header_x64(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent, COFF_MachineType machine, U64 base_addr, U64 sect_align, U64 file_align, Version linker_ver, Version os_ver, Version image_ver, Version subsystem_ver, PE_WindowsSubsystem subsystem, PE_DllCharacteristics dll_characteristics, U64 stack_reserve, U64 stack_commit, U64 heap_reserve, U64 heap_commit, String8 entry_point_name, LNK_SectionArray sect_arr);
internal LNK_Chunk * lnk_build_pe_directories(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent);
internal LNK_Chunk * lnk_build_coff_section_table(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent_chunk, LNK_SectionArray sect_arr);
internal LNK_Chunk * lnk_build_win32_image_header(LNK_SymbolTable *symtab, LNK_Section *header_sect, LNK_Chunk *parent_chunk, LNK_Config *config, LNK_SectionArray sect_arr);
////////////////////////////////
// Relocs
internal void lnk_patch_relocs(TP_Context *tp, LNK_SymbolTable *symtab, LNK_SectionTable *st, U64 base_addr);
internal void lnk_apply_reloc(U64 base_addr, U64 virt_align, U64 file_align, LNK_Section **sect_id_map, LNK_SymbolTable *symtab, String8 chunk_data, LNK_Reloc *reloc);
////////////////////////////////
internal void lnk_log_size_breakdown(LNK_SectionTable *st, LNK_SymbolTable *symtab);
internal void lnk_log_link_stats(LNK_ObjList obj_list, LNK_LibList *lib_index, LNK_SectionTable *st);
internal void lnk_log_timers(void);
+755
View File
@@ -0,0 +1,755 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
global read_only LNK_Chunk g_null_chunk = { 0, 0, /* is_discarded: */ 1 };
global read_only LNK_Chunk *g_null_chunk_ptr = &g_null_chunk;
internal LNK_ChunkRef
lnk_chunk_ref(U64 sect_id, U64 chunk_id)
{
LNK_ChunkRef ref = {0};
ref.sect_id = sect_id;
ref.chunk_id = chunk_id;
return ref;
}
internal B32
lnk_chunk_ref_is_equal(LNK_ChunkRef a, LNK_ChunkRef b)
{
B32 is_equal = a.sect_id == b.sect_id && a.chunk_id == b.chunk_id;
return is_equal;
}
internal LNK_ChunkNode *
lnk_chunk_list_push(Arena *arena, LNK_ChunkList *list, LNK_Chunk *chunk)
{
LNK_ChunkNode *node = push_array_no_zero(arena, LNK_ChunkNode, 1);
node->next = 0;
node->data = chunk;
SLLQueuePush(list->first, list->last, node);
++list->count;
return node;
}
internal void
lnk_chunk_list_concat_in_place(LNK_ChunkList *list, LNK_ChunkList *to_concat)
{
SLLConcatInPlace(list, to_concat);
}
internal void
lnk_chunk_list_concat_in_place_arr(LNK_ChunkList *list, LNK_ChunkList *arr, U64 count)
{
SLLConcatInPlaceArray(list, arr, count);
}
internal LNK_ChunkList **
lnk_make_chunk_list_arr_arr(Arena *arena, U64 slot_count, U64 per_count)
{
LNK_ChunkList **arr_arr = push_array_no_zero(arena, LNK_ChunkList *, slot_count);
for (U64 i = 0; i < slot_count; i += 1) {
arr_arr[i] = push_array(arena, LNK_ChunkList, per_count);
}
return arr_arr;
}
internal int
lnk_chunk_sort_index_is_before(void *raw_a, void *raw_b)
{
// Grouped Sections (PE Format)
// "All contributions with the same object-section name are allocated contiguously in the image,
// and the blocks of contributions are sorted in lexical order by object-section name."
LNK_ChunkPtr *a = raw_a;
LNK_ChunkPtr *b = raw_b;
// sort on section postfix
int cmp = str8_compar_case_sensetive(&(*a)->sort_idx, &(*b)->sort_idx);
// sort on obj position on command line
if (cmp == 0) {
cmp = u64_compar(&(*a)->input_idx, &(*b)->input_idx);
}
int is_before = cmp < 0;
return is_before;
}
internal void
lnk_chunk_array_sort(LNK_ChunkArray arr)
{
radsort(arr.v, arr.count, lnk_chunk_sort_index_is_before);
}
internal LNK_ChunkManager *
lnk_chunk_manager_alloc(Arena *arena, U64 id, U64 align)
{
ProfBeginFunction();
LNK_ChunkList temp_list = {0};
LNK_Chunk temp_chunk = {0};
temp_chunk.ref = lnk_chunk_ref(id, 0);
temp_chunk.align = align;
temp_chunk.type = LNK_Chunk_List;
temp_chunk.u.list = &temp_list;
LNK_ChunkManager *cman = push_array_no_zero(arena, LNK_ChunkManager, 1);
cman->total_chunk_count = 1; // null chunk
cman->root = 0;
cman->root = lnk_chunk_push_list(arena, cman, &temp_chunk, str8(0,0));
cman->root->align = align;
ProfEnd();
return cman;
}
internal LNK_Chunk *
lnk_chunk_push_(Arena *arena, LNK_Chunk *parent, U64 chunk_id, String8 sort_index)
{
ProfBeginFunction();
Assert(parent->type == LNK_Chunk_List);
LNK_ChunkList *list = parent->u.list;
LNK_Chunk *chunk = push_array_no_zero(arena, LNK_Chunk, 1);
chunk->ref = lnk_chunk_ref(parent->ref.sect_id, chunk_id);
chunk->align = 1;
chunk->is_discarded = 0;
chunk->sort_chunk = 1;
chunk->type = LNK_Chunk_Null;
chunk->sort_idx = push_str8_copy(arena, sort_index);
chunk->input_idx = list->count;
chunk->flags = 0;
chunk->associate = 0;
lnk_chunk_list_push(arena, list, chunk);
ProfEnd();
return chunk;
}
internal LNK_Chunk *
lnk_chunk_push(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index)
{
U64 chunk_id = cman->total_chunk_count;
++cman->total_chunk_count;
LNK_Chunk *chunk = lnk_chunk_push_(arena, parent, chunk_id, sort_index);
return chunk;
}
internal LNK_Chunk *
lnk_chunk_push_leaf(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index, void *raw_ptr, U64 raw_size)
{
LNK_Chunk *chunk = lnk_chunk_push(arena, cman, parent, sort_index);
chunk->type = LNK_Chunk_Leaf;
chunk->u.leaf = str8((U8 *)raw_ptr, raw_size);
return chunk;
}
internal LNK_Chunk *
lnk_chunk_push_list(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index)
{
LNK_Chunk *chunk = lnk_chunk_push(arena, cman, parent, sort_index);
chunk->type = LNK_Chunk_List;
chunk->u.list = push_array(arena, LNK_ChunkList, 1);
return chunk;
}
internal LNK_ChunkNode *
lnk_chunk_deep_copy(Arena *arena, LNK_Chunk *chunk)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
LNK_ChunkNode *dst_root_node = push_array_no_zero(arena, LNK_ChunkNode, 1);
LNK_ChunkNode *src_root_node = push_array_no_zero(scratch.arena, LNK_ChunkNode, 1);
src_root_node->next = 0;
src_root_node->data = chunk;
struct Stack {
struct Stack *next;
LNK_ChunkNode *src_node;
LNK_ChunkNode *dst_node;
};
struct Stack *stack = push_array_no_zero(scratch.arena, struct Stack, 1);
stack->next = 0;
stack->src_node = src_root_node;
stack->dst_node = dst_root_node;
while (stack) {
while (stack->src_node) {
LNK_Chunk *src = stack->src_node->data;
LNK_Chunk *dst = stack->dst_node->data;
stack->src_node = stack->src_node->next;
stack->dst_node = stack->dst_node->next;
dst->ref = src->ref;
dst->align = src->align;
dst->sort_idx = push_str8_copy(arena, src->sort_idx);
dst->type = src->type;
dst->flags = src->flags;
lnk_chunk_set_debugf(arena, dst, "%S", src->debug);
switch (src->type) {
case LNK_Chunk_Null: break;
case LNK_Chunk_Leaf: {
B32 is_bss = src->u.leaf.str == 0;
if (is_bss) {
dst->u.leaf = src->u.leaf;
} else {
dst->u.leaf = push_str8_copy(arena, src->u.leaf);
}
} break;
case LNK_Chunk_List: {
LNK_ChunkNode *chain = 0;
LNK_ChunkNode *curr = 0;
if (src->u.list->count > 0) {
chain = push_array(arena, LNK_ChunkNode, src->u.list->count);
curr = chain;
for (U64 i = 1; i < src->u.list->count; ++i) {
curr->next = &chain[i];
curr = curr->next;
}
curr->next = 0;
}
dst->u.list = push_array_no_zero(arena, LNK_ChunkList, 1);
dst->u.list->count = src->u.list->count;
dst->u.list->first = chain;
dst->u.list->last = curr;
struct Stack *frame = push_array_no_zero(scratch.arena, struct Stack, 1);
frame->next = 0;
frame->src_node = src->u.list->first;
frame->dst_node = dst->u.list->first;
SLLStackPush(stack, frame);
} break;
default: InvalidPath; break;
}
}
SLLStackPop(stack);
}
scratch_end(scratch);
ProfEnd();
return dst_root_node;
}
internal LNK_ChunkNode *
lnk_merge_chunks(Arena *arena, LNK_ChunkManager *dst_cman, LNK_Chunk *dst, LNK_Chunk *src, U64 *id_map_out, U64 id_map_max)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 0);
Assert(src->ref.sect_id != dst->ref.sect_id);
Assert(dst->type == LNK_Chunk_List);
Assert(src->type != LNK_Chunk_Null);
LNK_ChunkNode *src_node = push_array(arena, LNK_ChunkNode, 1);
src_node->data = src;
struct Stack {
struct Stack *next;
LNK_ChunkNode *node;
};
struct Stack *stack = push_array_no_zero(scratch.arena, struct Stack, 1);
stack->next = 0;
stack->node = src_node;
while (stack) {
while (stack->node) {
LNK_Chunk *chunk = stack->node->data;
// advance node
stack->node = stack->node->next;
// allocate id
U64 new_id = dst_cman->total_chunk_count++;
// write id map
Assert(chunk->ref.chunk_id < id_map_max);
id_map_out[chunk->ref.chunk_id] = new_id;
// update id
chunk->ref = lnk_chunk_ref(dst->ref.sect_id, new_id);
// recurse down on lists
if (chunk->type == LNK_Chunk_List) {
struct Stack *frame = push_array_no_zero(scratch.arena, struct Stack, 1);
frame->next = 0;
frame->node = chunk->u.list->first;
SLLStackPush(stack, frame);
}
}
// reached end of chunk list, pop frame
SLLStackPop(stack);
}
// move source root copy to destination section
LNK_ChunkList *list = dst->u.list;
++list->count;
SLLQueuePush(list->first, list->last, src_node);
scratch_end(scratch);
ProfEnd();
return src_node;
}
internal void
lnk_chunk_associate(Arena *arena, LNK_Chunk *head, LNK_Chunk *chunk)
{
// for simplicity we don't support multiple associations,
// but it's possible to craft symbol table with multiple associations
Assert(!chunk->associate);
chunk->associate = head;
}
internal B32
lnk_chunk_is_discarded(LNK_Chunk *chunk)
{
B32 is_discarded = chunk->is_discarded;
LNK_Chunk *curr = chunk->associate;
while (!is_discarded && curr) {
is_discarded = curr->is_discarded;
curr = curr->associate;
}
return is_discarded;
}
internal U64
lnk_chunk_get_size(LNK_Chunk *chunk)
{
U64 result = 0;
switch (chunk->type) {
case LNK_Chunk_Null: break;
case LNK_Chunk_Leaf: {
result = chunk->u.leaf.size;
} break;
case LNK_Chunk_LeafArray:
case LNK_Chunk_List: {
Assert(!"TODO: list size");
} break;
}
return result;
}
internal U64
lnk_chunk_list_get_node_count(LNK_Chunk *chunk)
{
Assert(chunk->type == LNK_Chunk_List);
return chunk->u.list->count;
}
internal void
lnk_chunk_op_list_push_node(LNK_ChunkOpList *list, LNK_ChunkOp *op)
{
SLLQueuePush(list->first, list->last, op);
}
internal LNK_ChunkOp *
lnk_push_chunk_op_begin(Arena *arena, U64 chunk_id)
{
LNK_ChunkOp *begin_op = push_array_no_zero(arena, LNK_ChunkOp, 1);
begin_op->next = 0;
begin_op->type = LNK_ChunkOp_Begin;
begin_op->u.chunk_id = chunk_id;
return begin_op;
}
internal LNK_ChunkOp *
lnk_push_chunk_op_end_virt(Arena *arena)
{
LNK_ChunkOp *end_virt_op = push_array_no_zero(arena, LNK_ChunkOp, 1);
end_virt_op->next = 0;
end_virt_op->type = LNK_ChunkOp_EndVirt;
return end_virt_op;
}
internal LNK_ChunkOp *
lnk_push_chunk_op_end_file(Arena *arena)
{
LNK_ChunkOp *end_op = push_array_no_zero(arena, LNK_ChunkOp, 1);
end_op->next = 0;
end_op->type = LNK_ChunkOp_End;
return end_op;
}
internal LNK_ChunkOp *
lnk_push_chunk_op_align(Arena *arena, U64 align, U64 val)
{
LNK_ChunkOp *align_op = push_array_no_zero(arena, LNK_ChunkOp, 1);
align_op->next = 0;
align_op->type = LNK_ChunkOp_Align;
align_op->u.align.x = align;
align_op->u.align.val = val;
return align_op;
}
internal LNK_ChunkOp *
lnk_push_chunk_op_write(Arena *arena, String8 string)
{
LNK_ChunkOp *write_op = push_array_no_zero(arena, LNK_ChunkOp, 1);
write_op->next = 0;
write_op->type = LNK_ChunkOp_WriteString;
write_op->u.string = string;
return write_op;
}
internal LNK_ChunkOpList
lnk_op_list_from_chunk(Arena *arena, LNK_Chunk *root, U64 total_chunk_count, U8 align_byte)
{
struct Stack {
struct Stack *next;
LNK_ChunkArray chunk_array;
U64 ichunk;
};
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
// setup stack
struct Stack *stack = push_array(scratch.arena, struct Stack, 1);
stack->chunk_array.count = 1;
stack->chunk_array.v = &root;
// setup output list
LNK_ChunkOpList list = {0};
list.total_chunk_count = total_chunk_count;
list.first = list.last = 0;
// write null
LNK_ChunkOp *null_begin_op = lnk_push_chunk_op_begin(arena, 0);
LNK_ChunkOp *null_end_virt_op = lnk_push_chunk_op_end_virt(arena);
LNK_ChunkOp *null_end_file_op = lnk_push_chunk_op_end_file(arena);;
lnk_chunk_op_list_push_node(&list, null_begin_op);
lnk_chunk_op_list_push_node(&list, null_end_virt_op);
lnk_chunk_op_list_push_node(&list, null_end_file_op);
// traverse chunks from root
while (stack) {
while (stack->ichunk < stack->chunk_array.count) {
LNK_Chunk *chunk = stack->chunk_array.v[stack->ichunk++];
// skip unused chunks
if (lnk_chunk_is_discarded(chunk)) {
continue;
}
switch (chunk->type) {
case LNK_Chunk_Leaf: {
// align start in its own begin/end block so align bytes don't contribute to chunk size
LNK_ChunkOp *pad_begin_op = lnk_push_chunk_op_begin(arena, list.total_chunk_count++);
LNK_ChunkOp *pad_align_op = lnk_push_chunk_op_align(arena, chunk->align, align_byte);
LNK_ChunkOp *pad_end_file_op = lnk_push_chunk_op_end_file(arena);
lnk_chunk_op_list_push_node(&list, pad_begin_op);
lnk_chunk_op_list_push_node(&list, pad_align_op);
lnk_chunk_op_list_push_node(&list, pad_end_file_op);
// write leaf
LNK_ChunkOp *leaf_begin_op = lnk_push_chunk_op_begin(arena, chunk->ref.chunk_id);
LNK_ChunkOp *leaf_write_op = lnk_push_chunk_op_write(arena, chunk->u.leaf);
LNK_ChunkOp *leaf_align_op = lnk_push_chunk_op_align(arena, chunk->align, align_byte);
LNK_ChunkOp *leaf_end_virt_op = lnk_push_chunk_op_end_virt(arena);
LNK_ChunkOp *leaf_end_file_op = lnk_push_chunk_op_end_file(arena);
#if LNK_DUMP_CHUNK_LAYOUT
leaf_write_op->chunk = chunk;
#endif
lnk_chunk_op_list_push_node(&list, leaf_begin_op);
lnk_chunk_op_list_push_node(&list, leaf_write_op);
lnk_chunk_op_list_push_node(&list, leaf_align_op);
lnk_chunk_op_list_push_node(&list, leaf_end_virt_op);
lnk_chunk_op_list_push_node(&list, leaf_end_file_op);
} break;
case LNK_Chunk_LeafArray: {
LNK_ChunkOp *begin_op = lnk_push_chunk_op_begin(arena, chunk->ref.chunk_id);
LNK_ChunkOp *align_op = lnk_push_chunk_op_align(arena, chunk->align, align_byte);
lnk_chunk_op_list_push_node(&list, begin_op);
lnk_chunk_op_list_push_node(&list, align_op);
if (chunk->sort_chunk) {
lnk_chunk_array_sort(*chunk->u.arr);
}
struct Stack *frame = push_array_no_zero(scratch.arena, struct Stack, 1);
frame->next = 0;
frame->chunk_array = *chunk->u.arr;
frame->ichunk = 0;
SLLStackPush(stack, frame);
} goto yeild;
case LNK_Chunk_List: {
// balance ops at :end_chunk_series
LNK_ChunkOp *begin_op = lnk_push_chunk_op_begin(arena, chunk->ref.chunk_id);
LNK_ChunkOp *align_op = lnk_push_chunk_op_align(arena, chunk->align, align_byte);
lnk_chunk_op_list_push_node(&list, begin_op);
lnk_chunk_op_list_push_node(&list, align_op);
// chunk list -> chunk array
LNK_ChunkArray chunk_array = {0};
chunk_array.v = push_array_no_zero(scratch.arena, LNK_ChunkPtr, chunk->u.list->count);
for (LNK_ChunkNode *cptr = chunk->u.list->first; cptr != 0; cptr = cptr->next) {
chunk_array.v[chunk_array.count++] = cptr->data;
}
if (chunk->sort_chunk) {
lnk_chunk_array_sort(chunk_array);
}
// recurse into list chunk
struct Stack *frame = push_array_no_zero(scratch.arena, struct Stack, 1);
frame->next = 0;
frame->chunk_array = chunk_array;
frame->ichunk = 0;
SLLStackPush(stack, frame);
} goto yeild;
case LNK_Chunk_Null: { /* ignore */ } break;
}
}
// terminate series
if (stack->next) {
struct Stack *prev = stack->next;
Assert(prev->ichunk > 0);
// :end_chunk_series
LNK_ChunkOp *end_virt_op = lnk_push_chunk_op_end_virt(arena);
LNK_ChunkOp *align_op = lnk_push_chunk_op_align(arena, prev->chunk_array.v[prev->ichunk - 1]->align, align_byte);
LNK_ChunkOp *end_op = lnk_push_chunk_op_end_file(arena);
lnk_chunk_op_list_push_node(&list, end_virt_op);
lnk_chunk_op_list_push_node(&list, align_op);
lnk_chunk_op_list_push_node(&list, end_op);
}
// move to next frame
SLLStackPop(stack);
yeild:;
}
scratch_end(scratch);
ProfEnd();
return list;
}
internal LNK_ChunkLayout
lnk_chunk_layout_from_op_list(Arena *arena, LNK_ChunkOpList op_list, B32 is_data_inited)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
// setup stack
struct Stack {
struct Stack *next;
U64 chunk_id;
U64 cursor;
} *stack = 0;
// setup state
U64 cursor = 0;
String8List data_list = {0};
// setup output
U64 *chunk_off_array = push_array_no_zero(arena, U64, op_list.total_chunk_count);
U64 *chunk_file_size_array = push_array_no_zero(arena, U64, op_list.total_chunk_count);
U64 *chunk_virt_size_array = push_array_no_zero(arena, U64, op_list.total_chunk_count);
// debug stomp so discarded chunks map to invalid offset
#if LNK_PARANOID
MemorySet(chunk_off_array, 0xFF, sizeof(*chunk_off_array) * op_list.total_chunk_count);
MemorySet(chunk_file_size_array, 0xFF, sizeof(*chunk_file_size_array) * op_list.total_chunk_count);
MemorySet(chunk_virt_size_array, 0xFF, sizeof(*chunk_virt_size_array) * op_list.total_chunk_count);
#endif
// execute opcodes
for (LNK_ChunkOp *op = op_list.first; op != NULL; op = op->next) {
switch (op->type) {
case LNK_ChunkOp_Null: break;
case LNK_ChunkOp_Begin: {
struct Stack *frame = push_array(scratch.arena, struct Stack, 1);
frame->chunk_id = op->u.chunk_id;
frame->cursor = cursor;
SLLStackPush(stack, frame);
chunk_off_array[stack->chunk_id] = stack->cursor;
} break;
case LNK_ChunkOp_End: {
chunk_file_size_array[stack->chunk_id] = cursor - stack->cursor;
SLLStackPop(stack);
} break;
case LNK_ChunkOp_EndVirt: {
chunk_virt_size_array[stack->chunk_id] = cursor - stack->cursor;
} break;
case LNK_ChunkOp_Align: {
Assert(IsPow2(op->u.align.x));
U64 size = AlignPow2(cursor, op->u.align.x) - cursor;
String8 string;
string.size = size;
string.str = push_array_no_zero(arena, U8, string.size);
MemorySet(string.str, op->u.align.val, string.size);
op->type = LNK_ChunkOp_WriteString;
op->u.string = string;
} // fall-through
case LNK_ChunkOp_WriteString: {
if (is_data_inited) {
// we allow chunks to have null for str for regions in the image that are zeroed out.
if (op->u.string.str == 0) {
op->u.string.str = push_array(arena, U8, op->u.string.size);
}
str8_list_push(scratch.arena, &data_list, op->u.string);
}
#if LNK_DUMP_CHUNK_LAYOUT
if (op->chunk) {
fprintf(g_layout_file, "[%.*s] %llX %.*s\n", str8_varg(op->chunk->sort_idx), op->chunk->input_idx, str8_varg(op->chunk->debug));
}
#endif
// advance
cursor += op->u.string.size;
} break;
}
}
// are begin/end series opcodes balanced?
Assert(stack == 0);
// fill out result
LNK_ChunkLayout layout = {0};
layout.data = str8_list_join(arena, &data_list, 0);
layout.chunk_off_array = chunk_off_array;
layout.chunk_file_size_array = chunk_file_size_array;
layout.chunk_virt_size_array = chunk_virt_size_array;
scratch_end(scratch);
ProfEnd();
return layout;
}
internal LNK_ChunkLayout
lnk_build_chunk_layout(Arena *arena, LNK_ChunkManager *cman, COFF_SectionFlags flags, U8 align_byte)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
// should we write data for chunks?
B32 is_data_inited = !!(~flags & COFF_SectionFlag_CNT_UNINITIALIZED_DATA);
// build layout
LNK_ChunkOpList op_list = lnk_op_list_from_chunk(scratch.arena, cman->root, cman->total_chunk_count, align_byte);
LNK_ChunkLayout layout = lnk_chunk_layout_from_op_list(arena, op_list, is_data_inited);
scratch_end(scratch);
ProfEnd();
return layout;
}
internal B32
lnk_visit_chunks_(U64 sect_id, LNK_Chunk *chunk, LNK_ChunkVisitorSig *cb, void *ud)
{
// visit chunk
B32 is_done = cb(sect_id, chunk, ud);
if (is_done) {
return is_done;
}
switch (chunk->type) {
case LNK_Chunk_Null:
case LNK_Chunk_Leaf: {
// reached leaf
} break;
case LNK_Chunk_LeafArray: {
for (U64 idx = 0; idx < chunk->u.arr->count; idx += 1) {
is_done = lnk_visit_chunks_(sect_id, chunk->u.arr->v[idx], cb, ud);
if (is_done) {
break;
}
}
} break;
case LNK_Chunk_List: {
for (LNK_ChunkNode *i = chunk->u.list->first; i != 0; i = i->next) {
is_done = lnk_visit_chunks_(sect_id, i->data, cb, ud);
if (is_done) {
break;
}
}
} break;
}
return is_done;
}
internal void
lnk_visit_chunks(U64 sect_id, LNK_Chunk *chunk, LNK_ChunkVisitorSig *cb, void *ud)
{
lnk_visit_chunks_(sect_id, chunk, cb, ud);
}
LNK_CHUNK_VISITOR_SIG(lnk_save_chunk_ptr)
{
LNK_Chunk **id_map = (LNK_Chunk **)ud;
if (!chunk->is_discarded) {
id_map[chunk->ref.chunk_id] = chunk;
}
return 0;
}
internal LNK_ChunkPtr *
lnk_make_chunk_id_map(Arena *arena, LNK_ChunkManager *cman)
{
LNK_ChunkPtr *chunk_id_map = push_array_no_zero(arena, LNK_ChunkPtr, cman->total_chunk_count + 1);
lnk_visit_chunks(0, cman->root, lnk_save_chunk_ptr, chunk_id_map);
LNK_Chunk *null_chunk = push_array(arena, LNK_Chunk, 1);
null_chunk->is_discarded = 1;
chunk_id_map[0] = null_chunk;
return chunk_id_map;
}
internal LNK_ChunkNode *
lnk_chunk_ptr_list_reserve(Arena *arena, LNK_ChunkList *list, U64 count)
{
LNK_ChunkNode *arr = 0;
if (count) {
arr = push_array(arena, LNK_ChunkNode, count);
LNK_Chunk *chunk_arr = push_array(arena, LNK_Chunk, count);
for (U64 i = 0; i < count; i += 1) {
arr[i].data = &chunk_arr[i];
SLLQueuePush(list->first, list->last, &arr[i]);
}
list->count += count;
}
return arr;
}
internal String8Array
lnk_data_arr_from_chunk_ptr_list(Arena *arena, LNK_ChunkList list)
{
String8Array arr = {0};
arr.v = push_array(arena, String8, list.count);
for (LNK_ChunkNode *n = list.first; n != 0; n = n->next) {
LNK_ChunkPtr c = n->data;
Assert(c->type == LNK_Chunk_Leaf);
arr.v[arr.count] = c->u.leaf;
arr.count += 1;
}
return arr;
}
internal String8Array *
lnk_data_arr_from_chunk_ptr_list_arr(Arena *arena, LNK_ChunkList *list_arr, U64 count)
{
String8Array *result = push_array(arena, String8Array, count);
for (U64 i = 0; i < count; i += 1) {
result[i] = lnk_data_arr_from_chunk_ptr_list(arena, list_arr[i]);
}
return result;
}
+164
View File
@@ -0,0 +1,164 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
////////////////////////////////
#define LNK_DEBUG_CHUNKS 0
#define LNK_DUMP_CHUNK_LAYOUT 0
////////////////////////////////
typedef struct LNK_ChunkRef
{
U64 sect_id;
U64 chunk_id;
} LNK_ChunkRef;
typedef enum
{
LNK_Chunk_Null,
LNK_Chunk_Leaf,
LNK_Chunk_LeafArray,
LNK_Chunk_List,
} LNK_ChunkType;
typedef struct LNK_Chunk
{
LNK_ChunkRef ref;
LNK_ChunkType type;
U64 align;
B32 is_discarded;
B32 sort_chunk;
String8 sort_idx;
U64 input_idx;
COFF_SectionFlags flags;
struct LNK_Chunk *associate;
union {
String8 leaf;
struct LNK_ChunkList *list;
struct LNK_ChunkArray *arr;
} u;
#if LNK_DEBUG_CHUNKS
String8 debug;
#endif
} LNK_Chunk, * LNK_ChunkPtr;
typedef struct LNK_ChunkNode
{
struct LNK_ChunkNode *next;
LNK_ChunkPtr data;
} LNK_ChunkNode;
typedef struct LNK_ChunkArray
{
U64 count;
LNK_ChunkPtr *v;
} LNK_ChunkArray;
typedef struct LNK_ChunkList
{
U64 count;
LNK_ChunkNode *first;
LNK_ChunkNode *last;
} LNK_ChunkList;
typedef enum LNK_ChunkOpType
{
LNK_ChunkOp_Null,
LNK_ChunkOp_WriteString,
LNK_ChunkOp_Align,
LNK_ChunkOp_Begin,
LNK_ChunkOp_End,
LNK_ChunkOp_EndVirt,
} LNK_ChunkOpType;
typedef struct LNK_ChunkOp
{
struct LNK_ChunkOp *next;
LNK_ChunkOpType type;
union {
String8 string;
U64 chunk_id;
struct {
U64 val;
U64 x;
} align;
LNK_Chunk *leaf;
} u;
#if LNK_DUMP_CHUNK_LAYOUT
LNK_Chunk *chunk;
#endif
} LNK_ChunkOp;
typedef struct LNK_ChunkOpList
{
U64 total_chunk_count;
LNK_ChunkOp *first;
LNK_ChunkOp *last;
} LNK_ChunkOpList;
typedef struct LNK_ChunkLayout
{
String8 data;
U64 *chunk_off_array;
U64 *chunk_file_size_array;
U64 *chunk_virt_size_array;
} LNK_ChunkLayout;
typedef struct LNK_ChunkManager
{
LNK_Chunk *root;
U64 total_chunk_count;
} LNK_ChunkManager;
extern LNK_Chunk g_null_chunk;
extern LNK_Chunk *g_null_chunk_ptr;
internal LNK_ChunkRef lnk_chunk_ref(U64 sect_id, U64 chunk_id);
internal B32 lnk_chunk_ref_is_equal(LNK_ChunkRef a, LNK_ChunkRef b);
internal LNK_ChunkNode * lnk_chunk_list_push(Arena *arena, LNK_ChunkList *list, LNK_Chunk *chunk);
internal void lnk_chunk_list_concat_in_place(LNK_ChunkList *list, LNK_ChunkList *to_concat);
internal void lnk_chunk_list_concat_in_place_arr(LNK_ChunkList *list, LNK_ChunkList *arr, U64 count);
internal LNK_ChunkList ** lnk_make_chunk_list_arr_arr(Arena *arena, U64 slot_count, U64 per_count);
internal void lnk_chunk_array_sort(LNK_ChunkArray arr);
internal LNK_ChunkManager * lnk_chunk_manager_alloc(Arena *arena, U64 id, U64 align);
internal LNK_Chunk * lnk_chunk_push(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index);
internal LNK_Chunk * lnk_chunk_push_leaf(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index, void *raw_ptr, U64 raw_size);
internal LNK_Chunk * lnk_chunk_push_list(Arena *arena, LNK_ChunkManager *cman, LNK_Chunk *parent, String8 sort_index);
internal LNK_ChunkNode * lnk_chunk_deep_copy(Arena *arena, LNK_Chunk *chunk);
internal LNK_ChunkNode * lnk_merge_chunks(Arena *arena, LNK_ChunkManager *dst_cman, LNK_Chunk *dst, LNK_Chunk *src, U64 *id_map_out, U64 id_map_max);
internal void lnk_chunk_associate(Arena *arena, LNK_Chunk *head, LNK_Chunk *associate);
internal B32 lnk_chunk_is_discarded(LNK_Chunk *chunk);
internal U64 lnk_chunk_get_size(LNK_Chunk *chunk);
internal U64 lnk_chunk_list_get_node_count(LNK_Chunk *chunk);
internal void lnk_chunk_op_list_push_node(LNK_ChunkOpList *list, LNK_ChunkOp *op);
internal LNK_ChunkOp * lnk_push_chunk_op_begin(Arena *arena, U64 chunk_id);
internal LNK_ChunkOp * lnk_push_chunk_op_end_virt(Arena *arena);
internal LNK_ChunkOp * lnk_push_chunk_op_end_file(Arena *arena);
internal LNK_ChunkOp * lnk_push_chunk_op_align(Arena *arena, U64 align, U64 val);
internal LNK_ChunkOp * lnk_push_chunk_op_write(Arena *arena, String8 string);
internal LNK_ChunkOpList lnk_op_list_from_chunk(Arena *arena, LNK_Chunk *root, U64 total_chunk_count, U8 align_byte);
internal LNK_ChunkLayout lnk_chunk_layout_from_op_list(Arena *arena, LNK_ChunkOpList op_list, B32 is_data_inited);
internal LNK_ChunkLayout lnk_build_chunk_layout(Arena *arena, LNK_ChunkManager *cman, COFF_SectionFlags flags, U8 align_byte);
#define LNK_CHUNK_VISITOR_SIG(name) B32 name(U64 sect_id, LNK_Chunk *chunk, void *ud)
typedef LNK_CHUNK_VISITOR_SIG(LNK_ChunkVisitorSig);
internal void lnk_visit_chunks(U64 sect_id, LNK_Chunk *root, LNK_ChunkVisitorSig *cb, void *ud);
internal LNK_ChunkNode * lnk_chunk_ptr_list_reserve(Arena *arena, LNK_ChunkList *list, U64 count);
internal String8Array lnk_data_arr_from_chunk_ptr_list(Arena *arena, LNK_ChunkList list);
internal String8Array * lnk_data_arr_from_chunk_ptr_list_arr(Arena *arena, LNK_ChunkList *list_arr, U64 count);
#if LNK_DEBUG_CHUNKS
#define lnk_chunk_set_debugf(a, c, f, ...) do { (c)->debug = push_str8f((a), f, __VA_ARGS__); } while(0)
#else
#define lnk_chunk_set_debugf(a, c, f, ...) (void)(c)
#endif
+274
View File
@@ -0,0 +1,274 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal String8List
lnk_arg_list_parse_windows_rules(Arena *arena, String8 string)
{
Temp scratch = scratch_begin(&arena, 1);
String8List list = {0};
U8 *ptr = string.str;
U8 *opl = string.str + string.size;
while (ptr < opl) {
// skip white space and new lines
for (;;) {
U64 size = (U64)(opl - ptr);
UnicodeDecode uni = utf8_decode(ptr, size);
if (uni.codepoint != ' ' && uni.codepoint != '\n' && uni.codepoint != '\r') {
break;
}
ptr += uni.inc;
}
if (*ptr == '\0') {
break;
}
String8List token_builder = {0};
U8 *anchor = ptr;
while (ptr < opl) {
UnicodeDecode uni;
uni = utf8_decode(ptr, (U64)(opl-ptr));
if (uni.codepoint == '\0' || uni.codepoint == '\n' || uni.codepoint == '\r' || uni.codepoint == ' ') {
break;
}
// handle string and strip quotes
if (uni.codepoint == '"') {
String8 text_before_quote = str8(anchor, (U64)(ptr - anchor));
str8_list_push(scratch.arena, &token_builder, text_before_quote);
// advance past starting quote
ptr += uni.inc;
anchor = ptr;
U8 *quote_end = ptr;
while (ptr < opl) {
uni = utf8_decode(ptr, (U64)(opl - ptr));
ptr += uni.inc;
// skip escape char
if (uni.codepoint == '\\') {
uni = utf8_decode(ptr, (U64)(opl - ptr));
ptr += uni.inc;
} else if (uni.codepoint == '"' || uni.codepoint == '\0') {
break; // found matching quote char
}
quote_end = ptr;
}
String8 text_inside_quotes = str8(anchor, (U64)(quote_end - anchor));
str8_list_push(scratch.arena, &token_builder, text_inside_quotes);
anchor = ptr;
} else {
ptr += uni.inc;
}
}
// push remaining text
String8 text = str8(anchor, (U64)(ptr - anchor));
str8_list_push(scratch.arena, &token_builder, text);
// push token
String8 token = str8_list_join(arena, &token_builder, NULL);
if (token.size) {
str8_list_push(arena, &list, token);
}
}
scratch_end(scratch);
return list;
}
internal void
lnk_cmd_line_push_option_node(LNK_CmdLine *cmd_line, LNK_CmdOption *opt)
{
SLLQueuePush(cmd_line->first_option, cmd_line->last_option, opt);
cmd_line->option_count += 1;
}
internal LNK_CmdOption *
lnk_cmd_line_push_option_list(Arena *arena, LNK_CmdLine *cmd_line, String8 string, String8List value_strings)
{
// fill out node
LNK_CmdOption *opt = push_array_no_zero(arena, LNK_CmdOption, 1);
opt->next = 0;
opt->string = string;
opt->value_strings = value_strings;
// push node
lnk_cmd_line_push_option_node(cmd_line, opt);
return opt;
}
internal LNK_CmdOption *
lnk_cmd_line_push_option_string(Arena *arena, LNK_CmdLine *cmd_line, String8 string, String8 value)
{
String8List value_list = str8_split_by_string_chars(arena, value, str8_lit(","), StringSplitFlag_KeepEmpties);
LNK_CmdOption *opt = lnk_cmd_line_push_option_list(arena, cmd_line, string, value_list);
return opt;
}
internal LNK_CmdOption *
lnk_cmd_line_push_option(Arena *arena, LNK_CmdLine *cmd_line, char *string, char *value)
{
return lnk_cmd_line_push_option_string(arena, cmd_line, str8_cstring(string), str8_cstring(value));
}
internal LNK_CmdOption *
lnk_cmd_line_push_option_if_not_present(Arena *arena, LNK_CmdLine *cmd_line, char *string, char *value)
{
if (!lnk_cmd_line_has_option(*cmd_line, string)) {
return lnk_cmd_line_push_option(arena, cmd_line, string, value);
}
return 0;
}
internal LNK_CmdLine
lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list)
{
Temp scratch = scratch_begin(&arena, 1);
LNK_CmdLine cmd_line = {0};
for (String8Node *arg_node = arg_list.first; arg_node != 0; arg_node = arg_node->next) {
String8 arg = arg_node->string;
B32 is_option = str8_match(str8_lit("/"), arg, StringMatchFlag_RightSideSloppy) ||
str8_match(str8_lit("-"), arg, StringMatchFlag_RightSideSloppy);
if (is_option) {
U64 param_start_pos = str8_find_needle(arg, 0, str8_lit(":"), 0);
String8 option_name = str8_chop(arg, arg.size - param_start_pos);
// remove '/' or '-' from option name
option_name = str8_skip(option_name, 1);
// skip ':'
String8 value_string = str8_skip(arg, param_start_pos + 1);
// make value list
String8List value_list = str8_split_by_string_chars(arena, value_string, str8_lit(","), 0);
// push command
lnk_cmd_line_push_option_list(arena, &cmd_line, option_name, value_list);
} else {
str8_list_push(arena, &cmd_line.input_list, arg);
}
}
scratch_end(scratch);
return cmd_line;
}
internal LNK_CmdOption *
lnk_cmd_line_option_from_string(LNK_CmdLine cmd_line, String8 string)
{
LNK_CmdOption *opt;
for (opt = cmd_line.first_option; opt != NULL; opt = opt->next) {
if (str8_match(string, opt->string, StringMatchFlag_CaseInsensitive)) {
break;
}
}
return opt;
}
internal B32
lnk_cmd_line_has_option_string(LNK_CmdLine cmd_line, String8 string)
{
LNK_CmdOption *opt = lnk_cmd_line_option_from_string(cmd_line, string);
B32 has_option = (opt != 0);
return has_option;
}
internal B32
lnk_cmd_line_has_option(LNK_CmdLine cmd_line, char *string)
{
return lnk_cmd_line_has_option_string(cmd_line, str8_cstring(string));
}
internal String8List
lnk_unwrap_rsp(Arena *arena, String8List arg_list)
{
Temp scratch = scratch_begin(&arena, 1);
String8List result = {0};
for (String8Node *curr = arg_list.first; curr != 0; curr = curr->next) {
B32 is_rsp = str8_match(str8_lit("@"), curr->string, StringMatchFlag_RightSideSloppy);
if (is_rsp) {
// remove "@"
String8 name = str8_skip(curr->string, 1);
if (os_file_path_exists(name)) {
// read rsp from disk
String8 file = os_data_from_file_path(scratch.arena, name);
// parse rsp
String8List rsp_args = lnk_arg_list_parse_windows_rules(scratch.arena, file);
// handle case where rsp references another rsp
String8List list = lnk_unwrap_rsp(arena, rsp_args);
// push arguments from rsp
list = str8_list_copy(arena, &list);
str8_list_concat_in_place(&result, &list);
} else {
lnk_error(LNK_Error_Cmdl, "unable to find rsp: %S", name);
}
} else {
// push regular argument
String8 str = push_str8_copy(arena, curr->string);
str8_list_push(arena, &result, str);
}
}
scratch_end(scratch);
return result;
}
internal String8List
lnk_data_from_cmd_line(Arena *arena, LNK_CmdLine cmd_line)
{
String8List result = {0};
for (LNK_CmdOption *opt = cmd_line.first_option; opt != 0; opt = opt->next) {
// separate directives
if (opt != cmd_line.first_option) {
str8_list_pushf(arena, &result, " ");
}
// push new directive
str8_list_pushf(arena, &result, "/%.*s", str8_varg(opt->string));
// do we have arguments?
if (opt->value_strings.node_count > 0) {
str8_list_pushf(arena, &result, ":");
for (String8Node *value_node = opt->value_strings.first; value_node != 0; value_node = value_node->next) {
// separate arguments
if (value_node != opt->value_strings.first) {
str8_list_pushf(arena, &result, ",");
}
// push argument
B32 has_spaces = str8_find_needle(value_node->string, 0, str8_lit(" "), StringMatchFlag_CaseInsensitive) < value_node->string.size;
if (has_spaces) {
str8_list_pushf(arena, &result, "\"%.*s\"", str8_varg(value_node->string));
} else {
str8_list_pushf(arena, &result, "%.*s", str8_varg(value_node->string));
}
}
}
}
// append inputs
for (String8Node *input_node = cmd_line.input_list.first; input_node != 0; input_node = input_node->next) {
if (input_node != cmd_line.input_list.first) {
str8_list_pushf(arena, &result, " ");
}
str8_list_pushf(arena, &result, "\"%.*s\"", str8_varg(input_node->string));
}
return result;
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct LNK_CmdOption
{
struct LNK_CmdOption *next;
String8 string;
String8List value_strings;
} LNK_CmdOption;
typedef struct LNK_CmdLine
{
U64 option_count;
LNK_CmdOption *first_option;
LNK_CmdOption *last_option;
String8List input_list;
} LNK_CmdLine;
internal String8List lnk_arg_list_parse_windows_rules(Arena *arena, String8 string);
internal LNK_CmdLine lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list);
internal LNK_CmdOption * lnk_cmd_line_option_from_string(LNK_CmdLine cmd_line, String8 string);
internal B32 lnk_cmd_line_has_option_string(LNK_CmdLine cmd_line, String8 string);
internal B32 lnk_cmd_line_has_option(LNK_CmdLine cmd_line, char *string);
internal LNK_CmdOption * lnk_cmd_line_push_option(Arena *arena, LNK_CmdLine *cmd_line, char *string, char *value);
internal LNK_CmdOption * lnk_cmd_line_push_option_if_not_present(Arena *arena, LNK_CmdLine *cmd_line, char *string, char *value);
internal String8List lnk_unwrap_rsp(Arena *arena, String8List arg_list);
internal String8List lnk_data_from_cmd_line(Arena *arena, LNK_CmdLine cmd_line);
File diff suppressed because it is too large Load Diff
+480
View File
@@ -0,0 +1,480 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum
{
LNK_CmdSwitch_Null,
LNK_CmdSwitch_NotImplemented,
LNK_CmdSwitch_Deprecated,
LNK_CmdSwitch_Align,
LNK_CmdSwitch_AllowBind,
LNK_CmdSwitch_AllowIsolation,
LNK_CmdSwitch_AlternateName,
LNK_CmdSwitch_AppContainer,
LNK_CmdSwitch_Base,
LNK_CmdSwitch_Debug,
LNK_CmdSwitch_DefaultLib,
LNK_CmdSwitch_Delay,
LNK_CmdSwitch_DelayLoad,
LNK_CmdSwitch_Dll,
LNK_CmdSwitch_DynamicBase,
LNK_CmdSwitch_Entry,
LNK_CmdSwitch_FastFail,
LNK_CmdSwitch_FileAlign,
LNK_CmdSwitch_Fixed,
LNK_CmdSwitch_FunctionPadMin,
LNK_CmdSwitch_Heap,
LNK_CmdSwitch_HighEntropyVa,
LNK_CmdSwitch_Ignore,
LNK_CmdSwitch_ImpLib,
LNK_CmdSwitch_Include,
LNK_CmdSwitch_Incremental,
LNK_CmdSwitch_LargeAddressAware,
LNK_CmdSwitch_LibPath,
LNK_CmdSwitch_Machine,
LNK_CmdSwitch_Manifest,
LNK_CmdSwitch_ManifestDependency,
LNK_CmdSwitch_ManifestFile,
LNK_CmdSwitch_ManifestInput,
LNK_CmdSwitch_ManifestUac,
LNK_CmdSwitch_Natvis,
LNK_CmdSwitch_NoDefaultLib,
LNK_CmdSwitch_NoLogo,
LNK_CmdSwitch_NxCompat,
LNK_CmdSwitch_Opt,
LNK_CmdSwitch_Out,
LNK_CmdSwitch_Pdb,
LNK_CmdSwitch_PdbPageSize,
LNK_CmdSwitch_Stack,
LNK_CmdSwitch_SubSystem,
LNK_CmdSwitch_Time,
LNK_CmdSwitch_TsAware,
// -- NOT Implemented:
LNK_CmdSwitch_AssemblyDebug,
LNK_CmdSwitch_AssemblyLinkResource,
LNK_CmdSwitch_AssemblyModule,
LNK_CmdSwitch_AssemblyResource,
LNK_CmdSwitch_ClrImageType,
LNK_CmdSwitch_ClrLoaderOptimization,
LNK_CmdSwitch_ClrSupportLastError,
LNK_CmdSwitch_ClrThreadAttribute,
LNK_CmdSwitch_ClrRunManagedCodeCheck,
LNK_CmdSwitch_ClrUnmanagedCheck,
LNK_CmdSwitch_Def,
LNK_CmdSwitch_DelaySign,
LNK_CmdSwitch_DependentLoadFlag,
LNK_CmdSwitch_Driver,
LNK_CmdSwitch_DisallowLib,
LNK_CmdSwitch_EmitVolatileMetadata,
LNK_CmdSwitch_ErrorReport,
LNK_CmdSwitch_Export,
LNK_CmdSwitch_ExportAdmin,
LNK_CmdSwitch_FastGenProfile,
LNK_CmdSwitch_Force,
LNK_CmdSwitch_Guard,
LNK_CmdSwitch_GenProfile,
LNK_CmdSwitch_IdlOut,
LNK_CmdSwitch_IgnoreIdl,
LNK_CmdSwitch_Ilk,
LNK_CmdSwitch_IntegrityCheck,
LNK_CmdSwitch_Kernel,
LNK_CmdSwitch_KeyContainer,
LNK_CmdSwitch_KeyFile,
LNK_CmdSwitch_LinkerRepro,
LNK_CmdSwitch_LinkerReproTarget,
LNK_CmdSwitch_Ltcg,
LNK_CmdSwitch_LtcgOut,
LNK_CmdSwitch_Map,
LNK_CmdSwitch_MapInfo,
LNK_CmdSwitch_Merge,
LNK_CmdSwitch_Midl,
LNK_CmdSwitch_NoAssembly,
LNK_CmdSwitch_NoEntry,
LNK_CmdSwitch_NoExp,
LNK_CmdSwitch_NoImpLib,
LNK_CmdSwitch_Order,
LNK_CmdSwitch_PdbStripped,
LNK_CmdSwitch_Profile,
LNK_CmdSwitch_Release,
LNK_CmdSwitch_SafeSeh,
LNK_CmdSwitch_Section,
LNK_CmdSwitch_SourceLink,
LNK_CmdSwitch_Stub,
LNK_CmdSwitch_SwapRun,
LNK_CmdSwitch_TlbId,
LNK_CmdSwitch_UserProfile,
LNK_CmdSwitch_Verbose,
LNK_CmdSwitch_Version,
LNK_CmdSwitch_Winmd,
LNK_CmdSwitch_WinmdDelaySign,
LNK_CmdSwitch_WinmdKeyContainer,
LNK_CmdSwitch_WinmdKeyFile,
LNK_CmdSwitch_WholeArchive,
LNK_CmdSwitch_Wx,
LNK_CmdSwitch_Rad_Age,
LNK_CmdSwitch_Rad_BuildInfo,
LNK_CmdSwitch_Rad_CheckUnusedDelayLoadDll,
LNK_CmdSwitch_Rad_Debug,
LNK_CmdSwitch_Rad_DebugName,
LNK_CmdSwitch_Rad_DelayBind,
LNK_CmdSwitch_Rad_DeleteManifest,
LNK_CmdSwitch_Rad_DoMerge,
LNK_CmdSwitch_Rad_EnvLib,
LNK_CmdSwitch_Rad_Exe,
LNK_CmdSwitch_Rad_Guid,
LNK_CmdSwitch_Rad_LargePages,
LNK_CmdSwitch_Rad_LinkVer,
LNK_CmdSwitch_Rad_Log,
LNK_CmdSwitch_Rad_Logo,
LNK_CmdSwitch_Rad_MtPath,
LNK_CmdSwitch_Rad_OsVer,
LNK_CmdSwitch_Rad_PageSize,
LNK_CmdSwitch_Rad_PathStyle,
LNK_CmdSwitch_Rad_SectVirtOff,
LNK_CmdSwitch_Rad_SuppressError,
LNK_CmdSwitch_Rad_SymbolTableCapDefined,
LNK_CmdSwitch_Rad_SymbolTableCapInternal,
LNK_CmdSwitch_Rad_SymbolTableCapWeak,
LNK_CmdSwitch_Rad_SymbolTableCapLib,
LNK_CmdSwitch_Rad_TargetOs,
LNK_CmdSwitch_Rad_TimeStamp,
LNK_CmdSwitch_Rad_Version,
LNK_CmdSwitch_Rad_Workers,
LNK_CmdSwitch_Help,
LNK_CmdSwitch_Count
} LNK_CmdSwitchType;
typedef enum
{
LNK_SwitchState_Null,
LNK_SwitchState_No,
LNK_SwitchState_Yes
} LNK_SwitchState;
typedef enum
{
LNK_Input_Null,
LNK_Input_Obj,
LNK_Input_Lib,
LNK_Input_Res,
LNK_Input_Manifest,
LNK_Input_Count
} LNK_InputType;
enum
{
LNK_ConfigFlag_Fixed = (1 << 0),
LNK_ConfigFlag_Merge = (1 << 1),
LNK_ConfigFlag_EnvLib = (1 << 2),
LNK_ConfigFlag_DelayUnload = (1 << 3),
LNK_ConfigFlag_DelayBind = (1 << 4),
LNK_ConfigFlag_CheckUnusedDelayLoadDll = (1 << 5),
LNK_ConfigFlag_NoTsAware = (1 << 6),
LNK_ConfigFlag_WriteImageChecksum = (1 << 8),
LNK_ConfigFlag_ManifestEmbed = (1 << 9),
};
typedef U64 LNK_ConfigFlags;
typedef enum
{
LNK_DebugMode_Null,
LNK_DebugMode_None,
LNK_DebugMode_FastLink,
LNK_DebugMode_GHash,
LNK_DebugMode_Full,
} LNK_DebugMode;
enum
{
LNK_Guard_None = 0,
LNK_Guard_Cf = (1 << 0),
LNK_Guard_LongJmp = (1 << 1),
LNK_Guard_EhCont = (1 << 2),
LNK_Guard_All = LNK_Guard_Cf | LNK_Guard_LongJmp | LNK_Guard_EhCont
};
typedef U32 LNK_GuardFlags;
typedef enum
{
LNK_ManifestOpt_Null,
LNK_ManifestOpt_Embed,
LNK_ManifestOpt_No
} LNK_ManifestOpt;
typedef struct LNK_AltNameList
{
String8List from_list;
String8List to_list;
} LNK_AltNameList;
typedef enum
{
LNK_DebugInfoGuid_Null,
Lnk_DebugInfoGuid_ImageBlake3,
} LNK_DebugInfoGuidType;
typedef struct LNK_Config
{
LNK_ConfigFlags flags;
LNK_DebugMode debug_mode;
LNK_SwitchState opt_ref;
LNK_SwitchState opt_icf;
LNK_SwitchState opt_lbr;
U64 opt_iter_count;
LNK_GuardFlags guard_flags;
LNK_DebugInfoGuidType guid_type;
OS_Guid guid;
COFF_TimeStamp time_stamp;
U32 age;
U64 section_virt_off;
U64 file_align;
U64 sect_align;
U64 stack_reserve;
U64 stack_commit;
U64 heap_reserve;
U64 heap_commit;
U64 user_base_addr;
U64 max_image_size;
U64 page_size;
U64 pdb_page_size;
U64 worker_count;
U64 function_pad_min;
U64 manifest_resource_id;
Version link_ver;
Version os_ver;
Version image_ver;
OperatingSystem target_os;
COFF_MachineType machine;
PE_WindowsSubsystem subsystem;
Version subsystem_ver;
PE_ImageFileCharacteristics file_characteristics;
PE_DllCharacteristics dll_characteristics;
String8 user_entry_point_name;
String8 entry_point_name;
String8List lib_dir_list;
PathStyle path_style;
LNK_ManifestOpt manifest_opt;
String8 work_dir;
String8 image_name;
String8 imp_lib_name;
String8List raw_cmd_line;
String8 pdb_name;
String8 mt_path;
String8List input_list[LNK_Input_Count];
String8List input_default_lib_list;
String8List disallow_lib_list;
String8List delay_load_dll_list;
String8List natvis_list;
LNK_SwitchState delete_manifest;
String8 manifest_name;
B32 manifest_uac;
String8 manifest_level;
String8 manifest_ui_access;
String8List manifest_dependency_list;
LNK_SwitchState rad_debug;
String8 rad_debug_name;
String8List include_symbol_list;
LNK_AltNameList alt_name_list;
U64 symbol_table_cap_defined;
U64 symbol_table_cap_internal;
U64 symbol_table_cap_weak;
U64 symbol_table_cap_lib;
} LNK_Config;
typedef enum
{
LNK_ParseU64Flag_CheckUnder32bit = (1 << 0),
LNK_ParseU64Flag_CheckPow2 = (1 << 1),
} LNK_ParseU64Flags;
////////////////////////////////
typedef enum
{
LNK_MsErrorCode_Lnk1000 = 1000,
LNK_MsErrorCode_Lnk1103 = 1103,
LNK_MsErrorCode_Lnk1104 = 1104,
LNK_MsErrorCode_Lnk1106 = 1106,
LNK_MsErrorCode_Lnk1107 = 1107,
LNK_MsErrorCode_Lnk1112 = 1112,
LNK_MsErrorCode_Lnk1113 = 1113,
LNK_MsErrorCode_Lnk1120 = 1120,
LNK_MsErrorCode_Lnk1123 = 1123,
LNK_MsErrorCode_Lnk1127 = 1127,
LNK_MsErrorCode_Lnk1136 = 1136,
LNK_MsErrorCode_Lnk1140 = 1140,
LNK_MsErrorCode_Lnk1141 = 1141,
LNK_MsErrorCode_Lnk1143 = 1143,
LNK_MsErrorCode_Lnk1152 = 1152,
LNK_MsErrorCode_Lnk1158 = 1158,
LNK_MsErrorCode_Lnk1164 = 1164,
LNK_MsErrorCode_Lnk1166 = 1166,
LNK_MsErrorCode_Lnk1168 = 1168,
LNK_MsErrorCode_Lnk1169 = 1169,
LNK_MsErrorCode_Lnk1170 = 1170,
LNK_MsErrorCode_Lnk1179 = 1179,
LNK_MsErrorCode_Lnk1181 = 1181,
LNK_MsErrorCode_Lnk1189 = 1189,
LNK_MsErrorCode_Lnk1196 = 1196,
LNK_MsErrorCode_Lnk1200 = 1200,
LNK_MsErrorCode_Lnk1201 = 1201,
LNK_MsErrorCode_Lnk1211 = 1211,
LNK_MsErrorCode_Lnk1215 = 1215,
LNK_MsErrorCode_Lnk1218 = 1218,
LNK_MsErrorCode_Lnk1221 = 1221,
LNK_MsErrorCode_Lnk1223 = 1223,
LNK_MsErrorCode_Lnk1224 = 1224,
LNK_MsErrorCode_Lnk1237 = 1237,
LNK_MsErrorCode_Lnk1240 = 1240,
LNK_MsErrorCode_Lnk1241 = 1241,
LNK_MsErrorCode_Lnk1245 = 1245,
LNK_MsErrorCode_Lnk1248 = 1248,
LNK_MsErrorCode_Lnk1256 = 1256,
LNK_MsErrorCode_Lnk1264 = 1264,
LNK_MsErrorCode_Lnk1277 = 1277,
LNK_MsErrorCode_Lnk1282 = 1282,
LNK_MsErrorCode_Lnk1287 = 1287,
LNK_MsErrorCode_Lnk1296 = 1296,
LNK_MsErrorCode_Lnk1301 = 1301,
LNK_MsErrorCode_Lnk1302 = 1302,
LNK_MsErrorCode_Lnk1306 = 1306,
LNK_MsErrorCode_Lnk1309 = 1309,
LNK_MsErrorCode_Lnk1312 = 1312,
LNK_MsErrorCode_Lnk1313 = 1313,
LNK_MsErrorCode_Lnk1314 = 1314,
LNK_MsErrorCode_Lnk1318 = 1318,
LNK_MsErrorCode_Lnk1332 = 1332,
LNK_MsErrorCode_Lnk1352 = 1352,
LNK_MsErrorCode_Lnk1561 = 1561,
LNK_MsErrorCode_Lnk2001 = 2001,
LNK_MsErrorCode_Lnk2004 = 2004,
LNK_MsErrorCode_Lnk2005 = 2005,
LNK_MsErrorCode_Lnk2008 = 2008,
LNK_MsErrorCode_Lnk2011 = 2011,
LNK_MsErrorCode_Lnk2013 = 2013,
LNK_MsErrorCode_Lnk2017 = 2017,
LNK_MsErrorCode_Lnk2019 = 2019,
LNK_MsErrorCode_Lnk2020 = 2020,
LNK_MsErrorCode_Lnk2022 = 2022,
LNK_MsErrorCode_Lnk2023 = 2023,
LNK_MsErrorCode_Lnk2026 = 2026,
LNK_MsErrorCode_Lnk2027 = 2027,
LNK_MsErrorCode_Lnk2031 = 2031,
LNK_MsErrorCode_Lnk2033 = 2033,
LNK_MsErrorCode_Lnk2038 = 2038,
LNK_MsErrorCode_Lnk2039 = 2039,
LNK_MsWarningCode_Lnk4001 = 4001,
LNK_MsWarningCode_Lnk4002 = 4002,
LNK_MsWarningCode_Lnk4006 = 4006,
LNK_MsWarningCode_Lnk4010 = 4010,
LNK_MsWarningCode_Lnk4014 = 4014,
LNK_MsWarningCode_Lnk4020 = 4020,
LNK_MsWarningCode_Lnk4022 = 4022,
LNK_MsWarningCode_Lnk4039 = 4039,
LNK_MsWarningCode_Lnk4044 = 4044,
LNK_MsWarningCode_Lnk4049 = 4049,
LNK_MsWarningCode_Lnk4065 = 4065,
LNK_MsWarningCode_Lnk4070 = 4070,
LNK_MsWarningCode_Lnk4071 = 4071,
LNK_MsWarningCode_Lnk4073 = 4073,
LNK_MsWarningCode_Lnk4075 = 4075,
LNK_MsWarningCode_Lnk4076 = 4076,
LNK_MsWarningCode_SectionFlagsConflict = 4078,
LNK_MsWarningCode_Lnk4086 = 4086,
LNK_MsWarningCode_Lnk4092 = 4092,
LNK_MsWarningCode_Lnk4096 = 4096,
LNK_MsWarningCode_Lnk4098 = 4098,
LNK_MsWarningCode_MissingExternalTypeServer = 4099,
LNK_MsWarningCode_Lnk4102 = 4102,
LNK_MsWarningCode_Lnk4104 = 4104,
LNK_MsWarningCode_Lnk4105 = 4105,
LNK_MsWarningCode_Lnk4194 = 4194,
LNK_MsWarningCode_Lnk4197 = 4197,
LNK_MsWarningCode_UnsuedDelayLoadDll = 4199,
LNK_MsWarningCode_Lnk4200 = 4200,
LNK_MsWarningCode_Lnk4204 = 4204,
LNK_MsWarningCode_Lnk4205 = 4205,
LNK_MsWarningCode_Lnk4206 = 4206,
LNK_MsWarningCode_Lnk4210 = 4210,
LNK_MsWarningCode_Lnk4216 = 4216,
LNK_MsWarningCode_Lnk4217 = 4217,
LNK_MsWarningCode_Lnk4219 = 4219,
LNK_MsWarningCode_Lnk4220 = 4220,
LNK_MsWarningCode_Lnk4221 = 4221,
LNK_MsWarningCode_Lnk4222 = 4222,
LNK_MsWarningCode_Lnk4224 = 4224,
LNK_MsWarningCode_Lnk4227 = 4227,
LNK_MsWarningCode_Lnk4229 = 4229,
LNK_MsWarningCode_Lnk4237 = 4237,
LNK_MsWarningCode_Lnk4247 = 4247,
LNK_MsWarningCode_Lnk4248 = 4248,
LNK_MsWarningCode_Lnk4253 = 4253,
LNK_MsWarningCode_Lnk4254 = 4254,
LNK_MsWarningCode_Lnk4286 = 4286,
} LNK_MsErrorCode;
////////////////////////////////
// Enum <-> String
internal String8 lnk_string_cmd_switch_type(LNK_CmdSwitchType type);
internal LNK_CmdSwitchType lnk_cmd_switch_from_string(String8 string);
internal LNK_InputType lnk_input_type_from_string(String8 string);
internal LNK_DebugMode lnk_debug_mode_from_string(String8 string);
////////////////////////////////
// Command Line Helpers
internal LNK_CmdOption * lnk_cmd_line_push_option_if_not_presentf(Arena *arena, LNK_CmdLine *cmd_line, LNK_CmdSwitchType cmd_switch_type, char *param_fmt, ...);
internal LNK_CmdOption * lnk_cmd_line_push_optionf(Arena *arena, LNK_CmdLine *cmd_line, LNK_CmdSwitchType cmd_switch_type, char *param_fmt, ...);
internal B32 lnk_cmd_line_has_switch(LNK_CmdLine cmd_line, LNK_CmdSwitchType cmd_switch_type);
////////////////////////////////
// Errors
internal void lnk_error_cmd_switch(LNK_ErrorCode code, LNK_CmdSwitchType cmd_switch, char *fmt, ...);
internal void lnk_error_cmd_switch_invalid_param_count(LNK_ErrorCode code, LNK_CmdSwitchType cmd_switch);
internal void lnk_error_cmd_switch_invalid_param(LNK_ErrorCode code, LNK_CmdSwitchType cmd_switch, String8 param);
////////////////////////////////
// Getters
internal U64 lnk_get_base_addr(LNK_Config *config);
internal Version lnk_get_default_subsystem_version(PE_WindowsSubsystem subsystem, COFF_MachineType machine);
internal Version lnk_get_min_subsystem_version(PE_WindowsSubsystem subsystem, COFF_MachineType machine);
internal String8 lnk_get_mt_path(Arena *arena);
internal B32 lnk_do_debug_info(LNK_Config *config);
////////////////////////////////
// Specialized Parsers
internal B32 lnk_cmd_switch_parse_version(String8List value_strings, LNK_CmdSwitchType cmd_switch, Version *ver_out);
internal B32 lnk_cmd_switch_parse_tuple(String8List value_strings, LNK_CmdSwitchType cmd_switch, Rng1U64 *tuple_out);
internal B32 lnk_cmd_switch_parse_u64(String8List value_strings, LNK_CmdSwitchType cmd_switch, U64 *value_out, LNK_ParseU64Flags flags);
internal B32 lnk_cmd_switch_parse_u32(String8List value_strings, LNK_CmdSwitchType cmd_switch, U32 *value_out, LNK_ParseU64Flags flags);
internal B32 lnk_cmd_switch_parse_flag(String8List value_strings, LNK_CmdSwitchType cmd_switch, LNK_SwitchState *value_out);
internal void lnk_cmd_switch_set_flag_inv_16(String8List value_strings, LNK_CmdSwitchType cmd_switch, U16 *flags, U16 bits);
internal void lnk_cmd_switch_set_flag_inv_64(String8List value_strings, LNK_CmdSwitchType cmd_switch, U64 *flags, U64 bits);
internal void lnk_cmd_switch_set_flag_16(String8List value_strings, LNK_CmdSwitchType cmd_switch, U16 *flags, U16 bits);
internal void lnk_cmd_switch_set_flag_32(String8List value_strings, LNK_CmdSwitchType cmd_switch, U32 *flags, U32 bits);
internal void lnk_cmd_switch_set_flag_64(String8List value_strings, LNK_CmdSwitchType cmd_switch, U64 *flags, U64 bits);
internal B32 lnk_cmd_switch_parse_string(String8List value_strings, LNK_CmdSwitchType cmd_switch, String8 *string_out);
internal void lnk_cmd_switch_parse_string_copy(Arena *arena, String8List value_strings, LNK_CmdSwitchType cmd_switch, String8 *string_out);
internal B32 lnk_parse_alt_name_directive(Arena *arena, String8 input, LNK_AltNameList *list_out);
internal String8 * lnk_parse_alt_name_directive_list(Arena *arena, String8List list, LNK_AltNameList *list_out);
////////////////////////////////
internal LNK_Config * lnk_config_from_raw_cmd_line(Arena *arena, String8List raw_cmd_line);
internal LNK_Config * lnk_build_config(Arena *arena, int argc, char **argv);
File diff suppressed because it is too large Load Diff
+609
View File
@@ -0,0 +1,609 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
////////////////////////////////
typedef struct LNK_PchInfo
{
CV_TypeIndex ti_lo;
CV_TypeIndex ti_hi;
U64 debug_p_obj_idx;
} LNK_PchInfo;
typedef struct LNK_CodeViewSymbolsInput
{
U64 obj_idx;
CV_SymbolList *symbol_list;
String8 raw_symbols;
} LNK_CodeViewSymbolsInput;
typedef struct LNK_CodeViewInput
{
U64 count;
U64 internal_count;
U64 external_count;
U64 type_server_count;
String8 *type_server_path_arr; // [type_server_count]
String8 *type_server_data_arr; // [type_server_count]
U64List *ts_to_obj_arr; // [type_server_count]
LNK_Obj *obj_arr; // [count]
LNK_PchInfo *pch_arr; // [count]
CV_DebugS *debug_s_arr; // [count]
CV_DebugT *debug_p_arr; // [count]
CV_DebugT *debug_t_arr; // [count]
CV_DebugT *merged_debug_t_p_arr; // [count]
U64 total_symbol_input_count;
LNK_CodeViewSymbolsInput *symbol_inputs; // [total_symbol_input_count]
CV_SymbolListArray *parsed_symbols; // [count]
LNK_Obj *internal_obj_arr; // [internal_count]
CV_DebugS *internal_debug_s_arr; // [internal_count]
CV_DebugT *internal_debug_t_arr; // [internal_count]
CV_DebugT *internal_debug_p_arr; // [internal_count]
U64 internal_total_symbol_input_count;
LNK_CodeViewSymbolsInput *internal_symbol_inputs; // [internal_total_symbol_input_count]
CV_SymbolListArray *internal_parsed_symbols; // [internal_count]
LNK_Obj *external_obj_arr; // [external_count]
CV_DebugS *external_debug_s_arr; // [external_count]
CV_DebugT *external_debug_t_arr; // [external_count]
CV_DebugT *external_debug_p_arr; // [external_count]
U64 external_total_symbol_input_count;
LNK_CodeViewSymbolsInput *external_symbol_inputs; // [exteranl_total_symbol_input_count]
CV_SymbolListArray *external_parsed_symbols; // [external_count]
Rng1U64 **external_ti_ranges; // [type_server_count]
CV_DebugT **external_leaves; // [type_server_count]
U64 *external_obj_to_ts_idx_arr; // [external_count]
Rng1U64 external_obj_range;
} LNK_CodeViewInput;
////////////////////////////////
typedef enum
{
LNK_LeafLocType_Internal,
LNK_LeafLocType_External,
LNK_LeafLocType_Count
} LNK_LeafLocType;
#define LNK_LeafRefFlag_LocIdxExternal (1 << 31)
#define LNK_LeafRefFlag_LeafIdxIPI (1 << 31)
typedef struct
{
U32 enc_loc_idx;
U32 enc_leaf_idx;
} LNK_LeafRef;
typedef struct LNK_LeafRange
{
struct LNK_LeafRange *next;
Rng1U64 range;
CV_DebugT *debug_t;
} LNK_LeafRange;
typedef struct LNK_LeafRangeList
{
U64 count;
LNK_LeafRange *first;
LNK_LeafRange *last;
} LNK_LeafRangeList;
typedef struct
{
LNK_LeafRef leaf_ref;
CV_TypeIndex type_index;
} LNK_LeafBucket;
typedef struct
{
U64 count;
LNK_LeafBucket **v;
} LNK_LeafBucketArray;
typedef struct
{
U64 cap;
LNK_LeafBucket **bucket_arr;
} LNK_LeafHashTable;
typedef union
{
struct {
U128Array **internal_hashes;
U128Array **external_hashes;
};
U128Array **v[CV_TypeIndexSource_COUNT];
} LNK_LeafHashes;
////////////////////////////////
typedef struct
{
LNK_Obj **obj_arr;
LNK_ChunkList *sect_list_arr;
CV_DebugS *debug_s_arr;
} LNK_ParseDebugSTaskData;
typedef struct
{
LNK_Obj **obj_arr;
String8Array *data_arr_arr;
} LNK_CheckDebugTSigTaskData;
typedef struct
{
LNK_Obj **obj_arr;
String8Array *data_arr_arr;
CV_DebugT *debug_t_arr;
} LNK_ParseDebugTTaskData;
typedef struct
{
String8Array data_arr;
MSF_Parsed **msf_parse_arr;
} LNK_MsfParsedFromDataTask;
typedef struct
{
CV_TypeServerInfo *ts_info_arr;
MSF_Parsed **msf_parse_arr;
Rng1U64 **external_ti_ranges;
CV_DebugT **external_leaves;
B8 *is_corrupted;
} LNK_GetExternalLeavesTask;
////////////////////////////////
typedef struct
{
LNK_LeafRangeList *leaf_ranges_per_task;
U64 **count_arr_arr;
} LNK_CountPerSourceLeafTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
Arena **fixed_arenas;
CV_DebugT *debug_t_arr;
} LNK_LeafHasherTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
LNK_LeafHashTable *leaf_ht_arr;
CV_DebugT *debug_t_arr;
} LNK_LeafDedupInternal;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
LNK_LeafHashTable *leaf_ht_arr;
CV_TypeIndexSource dedup_ti_source;
} LNK_LeafDedupExternal;
typedef struct
{
LNK_LeafHashTable *ht;
U64 *count_arr;
Rng1U64 *range_arr;
U64 *offset_arr;
LNK_LeafBucketArray result;
} LNK_GetPresentBucketsTask;
typedef struct
{
U64 loc_idx_bit_count_0;
U64 loc_idx_bit_count_1;
U64 loc_idx_bit_count_2;
U64 counts_max;
U32 **counts_arr;
Rng1U64 *ranges;
LNK_LeafBucket **dst;
LNK_LeafBucket **src;
U64 loc_idx_max;
U64 pass_idx;
} LNK_LeafRadixSortTask;
typedef struct
{
U32 *counts;
U32 *offsets;
LNK_LeafBucket **dst;
LNK_LeafBucket **src;
Rng1U64 *ranges;
} LNK_LeafLocRadixSortTask;
typedef struct
{
Rng1U64 *range_arr;
CV_TypeIndex min_type_index;
LNK_LeafBucketArray bucket_arr;
} LNK_AssignTypeIndicesTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafBucket **bucket_arr;
U8 **raw_leaf_arr;
Rng1U64 *range_arr;
} LNK_UnbucketRawLeavesTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
LNK_LeafHashTable *leaf_ht_arr;
CV_SymbolList *symbol_list_arr;
Arena **arena_arr;
} LNK_PatchSymbolTypesTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
LNK_LeafHashTable *leaf_ht_arr;
CV_DebugS *debug_s_arr;
} LNK_PatchInlinesTask;
typedef struct
{
LNK_CodeViewInput *input;
LNK_LeafHashes *hashes;
LNK_LeafHashTable *leaf_ht_arr;
LNK_LeafBucket **bucket_arr;
Rng1U64 *range_arr;
Arena **fixed_arena_arr;
} LNK_PatchLeavesTask;
////////////////////////////////
typedef struct
{
String8List *data_list_arr;
} LNK_ProcessedCodeViewC11Data;
typedef struct
{
String8List *data_list_arr;
String8List *source_file_names_list_arr;
} LNK_ProcessedCodeViewC13Data;
typedef struct
{
LNK_CodeViewSymbolsInput *inputs;
} LNK_ParseCVSymbolsTaskData;
typedef struct
{
U64 total_symbol_input_count;
LNK_CodeViewSymbolsInput *symbol_inputs;
CV_SymbolListArray *parsed_symbols;
PDB_DbiModule **mod_arr;
String8List *symbol_data_arr;
CV_SymbolList *gsi_list_arr;
} LNK_ProcessSymDataTaskData;
typedef struct
{
CV_DebugS *debug_s_arr;
MSF_Context *msf;
PDB_DbiModule **dbi_mod_arr;
String8List *c13_data_arr;
String8List *source_file_names_list_arr;
U64 string_data_base_offset;
CV_StringHashTable string_ht;
} LNK_ProcessC13DataTask;
typedef struct
{
MSF_Context *msf;
PDB_DbiModule **mod_arr;
String8List *symbol_data_arr;
String8List *c11_data_list_arr;
String8List *c13_data_list_arr;
String8List *globrefs_arr;
} LNK_WriteModuleDataTask;
typedef struct
{
LNK_Obj *obj_arr;
LNK_Section **sect_id_map;
PDB_DbiModule **mod_arr;
PDB_DbiSectionContribList *sc_list;
} LNK_PushDbiSecContribTaskData;
typedef struct
{
U32Array *hash_arr_arr;
CV_SymbolList *list_arr;
} LNK_HashCVSymbolListTask;
typedef struct
{
U64 *hash_arr;
CV_SymbolNode **arr;
Rng1U64 *range_arr;
} LNK_CvSymbolPtrArrayHasher;
typedef struct
{
LNK_Section **sect_id_map;
LNK_SymbolScopeIndex scope_idx;
LNK_SymbolList **bucket_arr;
CV_SymbolList *pub_list_arr;
Rng1U64 *symbol_ranges;
PDB_GsiContext *gsi;
CV_SymbolPtrArray symbols;
U32 *hashes;
} LNK_BuildPublicSymbolsTaskData;
typedef struct
{
CV_TypeIndex ipi_min_type_index;
CV_DebugT ipi_types;
LNK_CodeViewSymbolsInput *symbol_inputs;
CV_SymbolListArray *parsed_symbols;
} LNK_PostProcessCvSymbolsTask;
typedef struct
{
Rng1U64 *range_arr;
CV_SymbolPtrNode **bucket_arr;
CV_SymbolPtrNode **out_arr;
U64 *out_count_arr;
} LNK_GsiDeduper;
typedef struct
{
Rng1U64 *range_arr;
CV_SymbolPtrNode **bucket_arr;
U64 *symbol_base_arr;
CV_SymbolNode **symbol_arr;
} LNK_GsiUnbucket;
////////////////////////////////
// RAD Debug Info
typedef struct
{
String8 name;
U64 leaf_idx;
} LNK_UDTNameBucket;
typedef struct
{
CV_DebugT debug_t;
Rng1U64 *ranges;
U64 buckets_cap;
LNK_UDTNameBucket **buckets;
} LNK_BuildUDTNameHashTableTask;
typedef struct
{
CV_DebugT debug_t;
CV_TypeIndex ti_lo;
Rng1U64 *ranges;
U64 udt_name_buckets_cap;
LNK_UDTNameBucket **udt_name_buckets;
CV_TypeIndex *fwdmap;
} LNK_BuildUDTFwdMapTask;
////////////////////////////////
typedef struct
{
CV_DebugT *types;
U64 type_cap;
U64 udt_cap;
RDIB_TypeRef variadic_type_ref;
Rng1U64 *itype_ranges;
U64 udt_name_bucket_cap;
LNK_UDTNameBucket **udt_name_buckets;
RDIB_Type **tpi_itype_map;
RDIB_TypeChunkList *rdib_types_lists;
RDIB_TypeChunkList *rdib_types_struct_lists;
RDIB_TypeChunkList *rdib_types_union_lists;
RDIB_TypeChunkList *rdib_types_enum_lists;
RDIB_TypeChunkList *rdib_types_params_lists;
RDIB_TypeChunkList *rdib_types_udt_members_lists;
RDIB_TypeChunkList *rdib_types_enum_members_lists;
RDIB_UDTMemberChunkList *rdib_udt_members_lists;
RDIB_UDTMemberChunkList *rdib_enum_members_lists;
Rng1U64 *ranges;
} LNK_ConvertTypesToRDI;
typedef struct
{
U64 obj_idx;
RDIB_SourceFile *src_file;
} LNK_SourceFileBucket;
typedef struct
{
LNK_Obj *obj_arr;
CV_DebugS *debug_s_arr;
U64 total_src_file_count;
LNK_SourceFileBucket **src_file_buckets;
U64 src_file_buckets_cap;
} LNK_ConvertSourceFilesToRDITask;
typedef struct
{
CV_Arch arch;
CV_Language language;
String8 compiler_name;
} LNK_CodeViewCompilerInfo;
typedef struct
{
LNK_SectionArray image_sects;
LNK_Section **sect_id_map;
LNK_Obj *obj_arr;
CV_DebugS *debug_s_arr;
CV_DebugT ipi;
LNK_CodeViewSymbolsInput *symbol_inputs;
CV_SymbolListArray *parsed_symbols;
Rng1U64 ipi_itype_range;
Rng1U64 tpi_itype_range;
RDIB_Type **tpi_itype_map;
U64 src_file_buckets_cap;
LNK_SourceFileBucket **src_file_buckets;
LNK_UDTNameBucket **udt_name_buckets;
U64 line_table_cap;
U64 udt_name_buckets_cap;
U64 src_file_chunk_cap;
U64 symbol_chunk_cap;
U64 unit_chunk_cap;
U64 inline_site_cap;
RDIB_LineTable *null_line_table;
HashTable *extern_symbol_voff_ht;
LNK_CodeViewCompilerInfo *comp_info_arr;
CV_InlineeLinesAccel **inlinee_lines_accel_arr;
RDIB_InlineSiteChunk **inline_site_chunks;
// output
RDIB_UnitChunk *units;
RDIB_VariableChunkList *locals;
RDIB_ScopeChunkList *scopes;
RDIB_VariableChunkList *extern_gvars;
RDIB_VariableChunkList *static_gvars;
RDIB_VariableChunkList *extern_tvars;
RDIB_VariableChunkList *static_tvars;
RDIB_ProcedureChunkList *extern_procs;
RDIB_ProcedureChunkList *static_procs;
RDIB_InlineSiteChunkList *inline_sites;
RDIB_LineTableChunkList *line_tables;
} LNK_ConvertUnitToRDITask;
////////////////////////////////
// CodeView
internal CV_DebugS * lnk_parse_debug_s_sections(TP_Context *tp, TP_Arena *arena, U64 obj_count, LNK_Obj **obj_arr, LNK_ChunkList *sect_list_arr);
internal CV_DebugT * lnk_parse_debug_t_sections(TP_Context *tp, TP_Arena *arena, U64 obj_count, LNK_Obj **obj_arr, LNK_ChunkList *debug_t_list_arr);
internal CV_SymbolList * lnk_cv_symbol_list_arr_from_debug_s_arr(TP_Context *tp, TP_Arena *arena, U64 obj_count, CV_DebugS *debug_s_arr);
internal LNK_PchInfo * lnk_setup_pch(Arena *arena, U64 obj_count, LNK_Obj *obj_arr, CV_DebugT *debug_t_arr, CV_DebugT *debug_p_arr, CV_SymbolListArray *parsed_symbols);
internal LNK_CodeViewInput lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, String8List lib_dir_list, LNK_ObjList obj_list);
internal LNK_LeafRef lnk_leaf_ref(U32 idx, U32 leaf_idx);
internal LNK_LeafRef lnk_obj_leaf_ref(U32 obj_idx, U32 leaf_idx);
internal LNK_LeafRef lnk_ts_leaf_ref(CV_TypeIndexSource ti_source, U32 ts_idx, U32 leaf_idx);
internal int lnk_leaf_ref_compare(LNK_LeafRef a, LNK_LeafRef b);
internal LNK_LeafLocType lnk_loc_type_from_leaf_ref(LNK_LeafRef leaf_ref);
internal LNK_LeafLocType lnk_loc_type_from_obj_idx(LNK_CodeViewInput *input, U64 obj_idx);
internal U64 lnk_loc_idx_from_obj_idx(LNK_CodeViewInput *input, U64 obj_idx);
internal CV_TypeIndex lnk_ti_lo_from_loc(LNK_CodeViewInput *input, LNK_LeafLocType loc_type, U64 loc_idx, CV_TypeIndexSource ti_source);
internal CV_TypeIndex lnk_ti_lo_from_leaf_ref(LNK_CodeViewInput *input, LNK_LeafRef leaf_ref);
internal String8 lnk_data_from_leaf_ref(LNK_CodeViewInput *input, LNK_LeafRef leaf_ref);
internal CV_Leaf lnk_cv_leaf_from_leaf_ref(LNK_CodeViewInput *input, LNK_LeafRef leaf_ref);
internal U128 lnk_hash_from_leaf_ref(LNK_LeafHashes *hashes, LNK_LeafRef leaf_ref);
internal LNK_LeafRef lnk_leaf_ref_from_loc_idx_and_ti(LNK_CodeViewInput *input, LNK_LeafLocType loc_type, CV_TypeIndexSource ti_source, U64 loc_idx, CV_TypeIndex obj_ti);
internal B32 lnk_match_leaf_ref(LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafRef a, LNK_LeafRef b);
internal B32 lnk_match_leaf_ref_deep(Arena *arena, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafRef a, LNK_LeafRef b);
internal U128 lnk_hash_cv_leaf(Arena *arena, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafLocType loc_type, U32 loc_idx, Rng1U64 *ti_ranges, CV_TypeIndex curr_ti, CV_Leaf leaf, CV_TypeIndexInfoList ti_info_list);
internal void lnk_hash_cv_leaf_deep(Arena *arena, LNK_CodeViewInput *input, Rng1U64 *ti_ranges, CV_DebugT *leaves, LNK_LeafHashes *hashes, LNK_LeafLocType loc_type, U32 loc_idx, CV_TypeIndexInfoList ti_info_list, String8 data);
internal LNK_LeafBucket * lnk_leaf_hash_table_insert_or_update(LNK_LeafHashTable *leaf_ht, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, U128 hash, LNK_LeafBucket *new_bucket);
internal LNK_LeafBucket * lnk_leaf_hash_table_search(LNK_LeafHashTable *ht, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafRef leaf_ref);
internal void lnk_cv_debug_t_count_leaves_per_source(TP_Context *tp, U64 count, CV_DebugT *debug_t_arr, U64 *per_source_count_arr);
internal void lnk_hash_debug_t_arr(TP_Context *tp, Arena *arena, U64 obj_count, CV_DebugT *debug_t_arr, U128Array *hash_arr_arr);
internal LNK_LeafBucketArray lnk_present_bucket_array_from_leaf_hash_table(TP_Context *tp, Arena *arena, LNK_LeafHashTable *ht);
internal void lnk_leaf_bucket_array_sort_radix_subset_parallel(TP_Context *tp, U64 bucket_count, U64 loc_idx_max, LNK_LeafBucket **dst, LNK_LeafBucket **src);
internal void lnk_leaf_bucket_array_sort_radix_parallel(TP_Context *tp, LNK_LeafBucketArray arr, U64 obj_count, U64 type_server_count);
internal void lnk_assign_type_indices(TP_Context *tp, LNK_LeafBucketArray bucket_arr, CV_TypeIndex min_type_index);
internal void lnk_patch_symbols(TP_Context *tp, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafHashTable *leaf_ht_arr);
internal void lnk_patch_inlines(TP_Context *tp, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafHashTable *leaf_ht_arr, U64 obj_count, CV_DebugS *debug_s_arr);
internal void lnk_patch_leaves(TP_Context *tp, LNK_CodeViewInput *input, LNK_LeafHashes *hashes, LNK_LeafHashTable *leaf_ht_arr, LNK_LeafBucketArray bucket_arr);
internal String8Node * lnk_copy_raw_leaf_arr_to_type_server(TP_Context *tp, CV_DebugT types, PDB_TypeServer *type_server);
internal CV_DebugT * lnk_import_types(TP_Context *tp, TP_Arena *tp_temp, LNK_CodeViewInput *input);
////////////////////////////////
// RAD Debug info
internal U64 lnk_udt_name_hash_table_hash(String8 string);
internal LNK_UDTNameBucket ** lnk_udt_name_hash_table_from_debug_t(TP_Context *tp, TP_Arena *arena, CV_DebugT debug_t, U64 *buckets_cap_out);
internal LNK_UDTNameBucket * lnk_udt_name_hash_table_lookup(LNK_UDTNameBucket **buckets, U64 cap, String8 name);
internal CV_TypeIndex * lnk_build_udt_fwdmap(TP_Context *tp, Arena *arena, CV_DebugT debug_t, CV_TypeIndex ti_lo, LNK_UDTNameBucket **udt_name_buckets, U64 udt_name_buckets_cap);
internal RDIB_TypeRef lnk_rdib_type_from_itype(LNK_ConvertTypesToRDI *task, CV_TypeIndex itype);
internal RDI_MemberKind lnk_rdib_method_kind_from_cv_prop(CV_MethodProp prop);
internal LNK_SourceFileBucket * lnk_src_file_hash_table_hash(String8 file_path, CV_C13ChecksumKind checksum_kind, String8 checksum_bytes);
internal LNK_SourceFileBucket * lnk_src_file_hash_table_lookup_slot(LNK_SourceFileBucket **src_file_buckets, U64 src_file_buckets_cap, U64 hash, String8 file_path, CV_C13ChecksumKind checksum_kind, String8 checksum_bytes);
internal String8List lnk_build_rad_debug_info(TP_Context *tp,
TP_Arena *tp_arena,
OperatingSystem os,
RDI_Arch arch,
String8 image_name,
String8 image_data,
LNK_SectionArray image_sects,
LNK_Section **sect_id_map,
U64 obj_count,
LNK_Obj *obj_arr,
CV_DebugS *debug_s_arr,
U64 total_symbol_input_count,
LNK_CodeViewSymbolsInput *symbol_inputs,
CV_SymbolListArray *parsed_symbols,
CV_DebugT types[CV_TypeIndexSource_COUNT]);
////////////////////////////////
// PDB
internal LNK_ProcessedCodeViewC11Data lnk_process_c11_data(TP_Context *tp, TP_Arena *arena, U64 obj_count, CV_DebugS *debug_s_arr, U64 string_data_base_offset, CV_StringHashTable string_ht, MSF_Context *msf, PDB_DbiModule **mod_arr);
internal LNK_ProcessedCodeViewC13Data lnk_process_c13_data(TP_Context *tp, TP_Arena *arena, U64 obj_count, CV_DebugS *debug_s_arr, U64 string_data_base_offset, CV_StringHashTable string_ht, MSF_Context *msf, PDB_DbiModule **mod_arr);
internal U64 * lnk_hash_cv_symbol_ptr_arr(TP_Context *tp, Arena *arena, CV_SymbolPtrArray arr);
internal CV_SymbolPtrArray lnk_dedup_gsi_symbols(TP_Context *tp, Arena *arena, PDB_GsiContext *gsi, U64 obj_count, CV_SymbolList *symbol_list_arr);
internal void lnk_build_pdb_public_symbols(TP_Context *tp,
TP_Arena *arena,
LNK_SymbolTable *symtab,
LNK_Section **sect_id_map,
PDB_PsiContext *psi,
LNK_SymbolScopeIndex scope_idx);
internal String8List lnk_build_pdb(TP_Context *tp,
TP_Arena *tp_arena,
OS_Guid guid,
COFF_MachineType machine,
COFF_TimeStamp time_stamp,
U32 age,
U64 page_size,
String8 pdb_name,
String8List lib_dir_list,
String8List natvis_list,
LNK_SymbolTable *symtab,
LNK_Section **sect_id_map,
U64 obj_count,
LNK_Obj *obj_arr,
CV_DebugS *debug_s_arr,
U64 total_symbol_input_count,
LNK_CodeViewSymbolsInput *symbol_inputs,
CV_SymbolListArray *parsed_symbols,
CV_DebugT types[CV_TypeIndexSource_COUNT]);
////////////////////////////////
// RAD Debug Info
internal U64 lnk_udt_name_hash_table_hash(String8 string);
internal LNK_UDTNameBucket ** lnk_udt_name_hash_table_from_debug_t(TP_Context *tp, TP_Arena *arena, CV_DebugT debug_t, U64 *buckets_cap_out);
internal LNK_UDTNameBucket * lnk_udt_name_hash_table_lookup(LNK_UDTNameBucket **buckets, U64 cap, String8 name);
internal CV_TypeIndex * lnk_build_udt_fwdmap(TP_Context *tp,
Arena *arena,
CV_DebugT debug_t,
CV_TypeIndex ti_lo,
LNK_UDTNameBucket **udt_name_buckets,
U64 udt_name_buckets_cap);
internal void lnk_init_rdib_itype_map(Arena *arena, RDI_Arch arch, RDIB_Type **itype_map, RDIB_TypeChunkList *rdib_types_list);
internal RDIB_TypeRef lnk_rdib_type_from_itype(LNK_ConvertTypesToRDI *task, CV_TypeIndex itype);
internal RDI_MemberKind lnk_rdib_method_kind_from_cv_prop(CV_MethodProp prop);
+185
View File
@@ -0,0 +1,185 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal void
lnk_alt_name_list_concat_in_place(LNK_AltNameList *list, LNK_AltNameList *to_concat)
{
str8_list_concat_in_place(&list->from_list, &to_concat->from_list);
str8_list_concat_in_place(&list->to_list, &to_concat->to_list);
}
internal LNK_MergeDirectiveNode *
lnk_merge_directive_list_push(Arena *arena, LNK_MergeDirectiveList *list, LNK_MergeDirective data)
{
LNK_MergeDirectiveNode *node = push_array_no_zero(arena, LNK_MergeDirectiveNode, 1);
node->data = data;
node->next = 0;
SLLQueuePush(list->first, list->last, node);
++list->count;
return node;
}
////////////////////////////////
internal void
lnk_parse_directives(Arena *arena, LNK_DirectiveInfo *directive_info, String8 buffer, String8 obj_path)
{
Temp scratch = scratch_begin(&arena, 1);
String8 unparsed_directives = buffer;
{
static const U8 BOM_SIG[] = { 0xEF, 0xBB, 0xBF };
B32 is_bom = MemoryMatch(buffer.str, &BOM_SIG[0], sizeof(BOM_SIG));
if (is_bom) {
unparsed_directives = str8_zero();
lnk_not_implemented("TODO: support for BOM encoding");
}
static const U8 ASCII_SIG[] = { 0x20, 0x20, 0x20 };
B32 is_ascii = MemoryMatch(buffer.str, &ASCII_SIG[0], sizeof(ASCII_SIG));
if (is_ascii) {
unparsed_directives = str8_skip(buffer, sizeof(ASCII_SIG));
}
}
String8List arg_list = lnk_arg_list_parse_windows_rules(scratch.arena, unparsed_directives);
LNK_CmdLine cmd_line = lnk_cmd_line_parse_windows_rules(scratch.arena, arg_list);
for (LNK_CmdOption *opt = cmd_line.first_option; opt != 0; opt = opt->next) {
static struct {
LNK_DirectiveKind kind;
String8 name;
} directive_table[LNK_Directive_Count] = {
{ LNK_Directive_Null, str8_lit_comp("") },
{ LNK_Directive_DefaultLib, str8_lit_comp("defaultlib") },
{ LNK_Directive_Export, str8_lit_comp("export" ) },
{ LNK_Directive_Include, str8_lit_comp("include") },
{ LNK_Directive_ManifestDependency, str8_lit_comp("manifestdependency") },
{ LNK_Directive_Merge, str8_lit_comp("merge") },
{ LNK_Directive_Section, str8_lit_comp("section") },
{ LNK_Directive_AlternateName, str8_lit_comp("alternatename") },
{ LNK_Directive_GuardSym, str8_lit_comp("guardsym") },
{ LNK_Directive_DisallowLib, str8_lit_comp("disallowlib") },
{ LNK_Directive_FailIfMismatch, str8_lit_comp("failifmismatch") },
{ LNK_Directive_EditAndContinue, str8_lit_comp("editandcontinue") },
{ LNK_Directive_ThrowingNew, str8_lit_comp("throwingnew") },
};
LNK_DirectiveKind kind = LNK_Directive_Null;
for (U64 i = 0; i < ArrayCount(directive_table); ++i) {
if (str8_match(directive_table[i].name, opt->string, StringMatchFlag_CaseInsensitive)) {
kind = directive_table[i].kind;
break;
}
}
if (kind == LNK_Directive_Null) {
lnk_error(LNK_Warning_UnknownDirective, "%S: unknown directive \"%S\"", obj_path, opt->string);
}
LNK_Directive *directive = push_array_no_zero(arena, LNK_Directive, 1);
directive->next = 0;
directive->id = push_str8_copy(arena, opt->string);
directive->value_list = str8_list_copy(arena, &opt->value_strings);
LNK_DirectiveList *directive_list = &directive_info->v[kind];
SLLQueuePush(directive_list->first, directive_list->last, directive);
++directive_list->count;
}
scratch_end(scratch);
}
internal String8List
lnk_parse_default_lib_directive(Arena *arena, LNK_DirectiveList *dir_list)
{
ProfBeginFunction();
String8List default_libs = {0};
for (LNK_Directive *dir = dir_list->first; dir != 0; dir = dir->next) {
for (String8Node *i = dir->value_list.first; i != 0; i = i->next) {
String8 lib_path = i->string;
// is there lib extension?
String8 ext = str8_skip_last_dot(lib_path);
if (ext.size == lib_path.size) { // TODO: fix string_extension_from_path, if there is no extension it should return zero
lib_path = push_str8f(arena, "%S.lib", lib_path);
} else {
lib_path = push_str8_copy(arena, lib_path);
}
str8_list_push(arena, &default_libs, lib_path);
}
}
ProfEnd();
return default_libs;
}
internal LNK_ExportParse *
lnk_parse_export_direcive(Arena *arena, LNK_ExportParseList *list, String8List value_list, LNK_Obj *obj)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
LNK_ExportParse *parse = 0;
// parse directive
String8 name = str8(0,0);
String8 alias = str8(0,0);
String8 type = coff_string_from_import_header_type(COFF_ImportHeaderType_CODE);
if (value_list.node_count > 0) {
String8List dir_split = str8_split_by_string_chars(scratch.arena, value_list.first->string, str8_lit("="), 0);
B32 is_export_valid = value_list.node_count <= 2 && value_list.node_count > 0;
if (is_export_valid) {
if (dir_split.node_count > 0) {
name = dir_split.last->string;
}
if (dir_split.node_count == 2) {
alias = dir_split.first->string;
}
if (value_list.node_count == 2) {
type = value_list.last->string;
}
}
}
// prase error check
if (name.size == 0) {
String8 dir = str8_list_join(scratch.arena, &value_list, 0);
lnk_error_obj(LNK_Error_IllData, obj, "invalid export directive \"%S\"", dir);
goto exit;
}
parse = push_array_no_zero(arena, LNK_ExportParse, 1);
parse->next = 0;
parse->name = name;
parse->alias = alias;
parse->type = type;
SLLQueuePush(list->first, list->last, parse);
++list->count;
exit:;
scratch_end(scratch);
ProfEnd();
return parse;
}
internal B32
lnk_parse_merge_directive(String8 string, LNK_MergeDirective *out)
{
Temp scratch = scratch_begin(0, 0);
B32 is_parse_ok = 0;
String8List list = str8_split_by_string_chars(scratch.arena, string, str8_lit("="), 0);
if (list.node_count == 2) {
out->src = list.first->string;
out->dst = list.last->string;
is_parse_ok = 1;
}
scratch_end(scratch);
return is_parse_ok;
}
+89
View File
@@ -0,0 +1,89 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct LNK_Directive
{
struct LNK_Directive *next;
String8 id;
String8List value_list;
} LNK_Directive;
typedef struct LNK_DirectiveList
{
U64 count;
LNK_Directive *first;
LNK_Directive *last;
} LNK_DirectiveList;
typedef struct LNK_ExportParse
{
struct LNK_ExportParse *next;
String8 name;
String8 alias;
String8 type;
} LNK_ExportParse;
typedef struct LNK_ExportParseList
{
U64 count;
LNK_ExportParse *first;
LNK_ExportParse *last;
} LNK_ExportParseList;
typedef struct LNK_MergeDirective
{
String8 src;
String8 dst;
} LNK_MergeDirective;
typedef struct LNK_MergeDirectiveNode
{
struct LNK_MergeDirectiveNode *next;
LNK_MergeDirective data;
} LNK_MergeDirectiveNode;
typedef struct LNK_MergeDirectiveList
{
U64 count;
LNK_MergeDirectiveNode *first;
LNK_MergeDirectiveNode *last;
} LNK_MergeDirectiveList;
typedef enum
{
LNK_Directive_Null,
LNK_Directive_DefaultLib,
LNK_Directive_Export,
LNK_Directive_Include,
LNK_Directive_ManifestDependency,
LNK_Directive_Merge,
LNK_Directive_Section,
LNK_Directive_AlternateName,
LNK_Directive_GuardSym,
LNK_Directive_DisallowLib,
LNK_Directive_FailIfMismatch,
LNK_Directive_EditAndContinue,
LNK_Directive_ThrowingNew,
LNK_Directive_Count
} LNK_DirectiveKind;
typedef struct LNK_DirectiveInfo
{
LNK_DirectiveList v[LNK_Directive_Count];
} LNK_DirectiveInfo;
////////////////////////////////
internal void lnk_alt_name_list_concat_in_place(LNK_AltNameList *list, LNK_AltNameList *to_concat);
internal LNK_MergeDirectiveNode * lnk_merge_directive_list_push(Arena *arena, LNK_MergeDirectiveList *list, LNK_MergeDirective data);
////////////////////////////////
internal void lnk_parse_directives(Arena *arena, LNK_DirectiveInfo *directive_info, String8 buffer, String8 obj_path);
internal String8List lnk_parse_default_lib_directive(Arena *arena, LNK_DirectiveList *dir_list);
internal B32 lnk_parse_merge_directive(String8 directive, LNK_MergeDirective *out);
+121
View File
@@ -0,0 +1,121 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
static LNK_ErrorMode g_error_mode_arr[LNK_Error_Count];
static LNK_ErrorCodeStatus g_error_code_status_arr[LNK_Error_Count];
static B32 g_log_status[LNK_Log_Count];
internal void
lnk_exit(int code)
{
exit(code);
}
internal void
lnk_init_error_handler(void)
{
for (int i = LNK_Error_StopFirst; i < LNK_Error_StopLast; ++i) {
g_error_mode_arr[i] = LNK_ErrorMode_Stop;
}
for (int i = LNK_Error_First; i < LNK_Error_Last; ++i) {
g_error_mode_arr[i] = LNK_ErrorMode_Continue;
}
for (int i = LNK_Warning_First; i < LNK_Warning_Last; ++i) {
g_error_mode_arr[i] = LNK_ErrorMode_Warn;
}
}
internal String8
lnk_string_from_error_mode(LNK_ErrorMode mode)
{
switch (mode) {
case LNK_ErrorMode_Ignore: return str8_lit("Ignore");
case LNK_ErrorMode_Continue: return str8_lit("Error");
case LNK_ErrorMode_Stop: return str8_lit("Error");
case LNK_ErrorMode_Warn: return str8_lit("Warning");
}
return str8_zero();
}
internal void
lnk_errorfv(LNK_ErrorCode code, char *fmt, va_list args)
{
if (g_error_mode_arr[code] == LNK_ErrorMode_Ignore) {
return;
}
if (lnk_is_error_code_ignored(code)) {
return;
}
Temp scratch = scratch_begin(0,0);
String8 message = push_str8fv(scratch.arena, fmt, args);
String8 string = push_str8f(scratch.arena, "%S(%03d): %S\n", lnk_string_from_error_mode(g_error_mode_arr[code]), code, message);
fprintf(stderr, "%.*s", str8_varg(string));
scratch_end(scratch);
if (g_error_mode_arr[code] == LNK_ErrorMode_Stop) {
lnk_exit(code);
}
}
internal void
lnk_error(LNK_ErrorCode code, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
lnk_errorfv(code, fmt, args);
va_end(args);
}
internal void
lnk_supplement_error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
Temp scratch = scratch_begin(0,0);
String8 string = push_str8fv(scratch.arena, fmt, args);
fprintf(stderr, "\t");
fprintf(stderr, "%.*s", str8_varg(string));
fprintf(stderr, "\n");
va_end(args);
scratch_end(scratch);
}
internal void
lnk_supplement_error_list(String8List list)
{
for (String8Node *node = list.first; node != 0; node = node->next) {
lnk_supplement_error("%.*s", str8_varg(node->string));
}
}
internal void
lnk_suppress_error(LNK_ErrorCode code)
{
g_error_code_status_arr[code] = LNK_ErrorCodeStatus_Ignore;
}
internal LNK_ErrorCodeStatus
lnk_get_error_code_status(LNK_ErrorCode code)
{
return g_error_code_status_arr[code];
}
internal void
lnk_internal_error(LNK_InternalError code, char *file, int line, char *fmt, ...)
{
Temp scratch = scratch_begin(0,0);
va_list args;
va_start(args, fmt);
String8 issue = push_str8fv(scratch.arena, fmt, args);
fprintf(stderr, "internal error #%03d in %s:%u\n", code, file, line);
fprintf(stderr, "\t%.*s\n", str8_varg(issue));
va_end(args);
scratch_end(scratch);
}
+121
View File
@@ -0,0 +1,121 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum
{
LNK_Error_Null,
LNK_Error_StopFirst,
LNK_Error_Cmdl,
LNK_Error_EndprecompNotFound,
LNK_Error_EntryPoint,
LNK_Error_ExternalTypeServerConflict,
LNK_Error_FileNotFound,
LNK_Error_IllData,
LNK_Error_IllExport,
LNK_Error_IncomatibleCmdOptions,
LNK_Error_IncompatibleObj,
LNK_Error_InvalidPrecompLeafCount,
LNK_Error_InvalidStartIndex,
LNK_Error_NoAccess,
LNK_Error_NoSubsystem,
LNK_Error_OutOfExportOrdinals,
LNK_Error_PrecompObjNotFound,
LNK_Error_PrecompSigMismatch,
LNK_Error_Telemetry,
LNK_Error_UnsupportedMachine,
LNK_Error_Mt,
LNK_Error_UnableToSerializeMsf,
LNK_Error_StopLast,
LNK_Error_First,
LNK_Error_InvalidPath,
LNK_Error_AlreadyDefinedSymbol,
LNK_Error_AlternateNameConflict,
LNK_Error_CvPrecomp,
LNK_Error_MultiplyDefinedSymbol,
LNK_Error_Natvis,
LNK_Error_TooManyFiles,
LNK_Error_UndefinedSymbol,
LNK_Error_UnresolvedSymbol,
LNK_Error_UnableToOpenTypeServer,
LNK_Error_UnexpectedCodePath,
LNK_Error_CvIllSymbolData,
LNK_Error_IllegalAlternateNameRedifine,
LNK_Error_InvalidTypeIndex,
LNK_Error_Last,
LNK_Warning_First,
LNK_Warning_AmbiguousMerge,
LNK_Warning_AtypicalStartIndex,
LNK_Warning_Cmdl,
LNK_Warning_Directive,
LNK_Warning_DuplicateObjPath,
LNK_Warning_ExternalTypeServerAgeMismatch,
LNK_Warning_FileNotFound,
LNK_Warning_IllData,
LNK_Warning_IllExport,
LNK_Warning_InvalidNatvisFileExt,
LNK_Warning_LargePages,
LNK_Warning_LargePagesNotEnabled,
LNK_Warning_MismatchedTypeServerSignature,
LNK_Warning_MissingExternalTypeServer,
LNK_Warning_MultipleDebugTAndDebugP,
LNK_Warning_MultipleExternalTypeServers,
LNK_Warning_MultipleLibMatch,
LNK_Warning_MultiplyDefinedImport,
LNK_Warning_Natvis,
LNK_Warning_PrecompObjSymbolsNotFound,
LNK_Warning_SectionFlagsConflict,
LNK_Warning_Subsystem,
LNK_Warning_UnknownDirective,
LNK_Warning_UnresolvedComdat,
LNK_Warning_UnusedDelayLoadDll,
LNK_Warning_LongSectionName,
LNK_Warning_UnknownSwitch,
LNK_Warning_Last,
LNK_Error_Count
} LNK_ErrorCode;
typedef enum
{
LNK_ErrorMode_Ignore,
LNK_ErrorMode_Stop,
LNK_ErrorMode_Continue,
LNK_ErrorMode_Warn,
} LNK_ErrorMode;
typedef enum
{
LNK_InternalError_Null,
LNK_InternalError_NotImplemented,
LNK_InternalError_InvalidPath,
LNK_InternalError_IncompleteSwitch,
LNK_InternalError_OutOfMemory
} LNK_InternalError;
typedef enum
{
LNK_ErrorCodeStatus_Active,
LNK_ErrorCodeStatus_Ignore,
} LNK_ErrorCodeStatus;
internal void lnk_init_error_handler(void);
internal void lnk_errorfv(LNK_ErrorCode code, char *fmt, va_list args);
internal void lnk_error(LNK_ErrorCode code, char *fmt, ...);
internal void lnk_supplement_error(char *fmt, ...);
internal void lnk_supplement_error_list(String8List list);
internal void lnk_suppress_error(LNK_ErrorCode code);
#define lnk_is_error_code_active(code) (lnk_get_error_code_status(code) == LNK_ErrorCodeStatus_Active)
#define lnk_is_error_code_ignored(code) (lnk_get_error_code_status(code) == LNK_ErrorCodeStatus_Ignore)
internal LNK_ErrorCodeStatus lnk_get_error_code_status(LNK_ErrorCode code);
internal void lnk_internal_error(LNK_InternalError code, char *file, int line, char *fmt, ...);
#define lnk_invalid_path(fmt, ...) lnk_internal_error(LNK_InternalError_InvalidPath, __FILE__, __LINE__, fmt, __VA_ARGS__)
#define lnk_not_implemented(fmt, ...) lnk_internal_error(LNK_InternalError_NotImplemented, __FILE__, __LINE__, fmt, __VA_ARGS__)
#define lnk_incomplete_switch(fmt, ...) lnk_internal_error(LNK_InternalError_IncompleteSwitch, __FILE__, __LINE__, fmt, __VA_ARGS__)
+302
View File
@@ -0,0 +1,302 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
int
lnk_export_name_compar(const void *a_, const void *b_)
{
const LNK_Export *a = (const LNK_Export *)a_;
const LNK_Export *b = (const LNK_Export *)b_;
return str8_compar_case_sensetive(&a->name, &b->name);
}
int
lnk_export_ordinal_compar(const void *a_, const void *b_)
{
const LNK_Export *a = (const LNK_Export *)a_;
const LNK_Export *b = (const LNK_Export *)b_;
int cmp = u16_compar(&a->ordinal, &b->ordinal);
return cmp;
}
internal LNK_ExportTable *
lnk_export_table_alloc(void)
{
ProfBeginFunction();
Arena *arena = arena_alloc();
LNK_ExportTable *exptab = push_array(arena, LNK_ExportTable, 1);
exptab->arena = arena;
exptab->voff_size = sizeof(U32);
exptab->max_ordinal = max_U16;
exptab->is_ordinal_used = push_array(arena, B8, exptab->max_ordinal);
ProfEnd();
return exptab;
}
internal void
lnk_export_table_release(LNK_ExportTable **exptab_ptr)
{
ProfBeginFunction();
arena_release((*exptab_ptr)->arena);
*exptab_ptr = NULL;
ProfEnd();
}
internal LNK_Export *
lnk_export_table_search(LNK_ExportTable *exptab, String8 name)
{
for (LNK_Export *exp = exptab->name_export_list.first; exp != NULL; exp = exp->next) {
if (str8_match(exp->name, name, 0)) {
return exp;
}
}
return NULL;
}
internal LNK_Export *
lnk_export_table_push_export(LNK_ExportTable *exptab, LNK_SymbolTable *symtab, LNK_ExportParse *exp_parse)
{
LNK_Export *exp = 0;
// get export symbol
LNK_Symbol *symbol = lnk_symbol_table_search(symtab, LNK_SymbolScopeFlag_Main, exp_parse->name);
if (symbol == 0) {
lnk_error(LNK_Warning_IllExport, "symbol \"%S\" for export doesn't exist", exp_parse->name);
goto exit;
}
symbol = lnk_resolve_symbol(symtab, symbol);
if (!LNK_Symbol_IsDefined(symbol->type)) {
lnk_error(LNK_Warning_IllExport, "unable to resolve symbol \"%S\" for export", exp_parse->name);
goto exit;
}
LNK_DefinedSymbol *def = &symbol->u.defined;
// NOTE: It is possible to export a global variable as CODE
// with following snippet:
// int global_bar = 0;
// #pragma comment(linker, "/export:global_bar")
// for some reason MSVC and LLD don't check symbol type and default
// to CODE instead of DATA. But if you try export global variable with:
// #pragma comment(linker, "/export:global_bar,CODE")
// MSVC and LLD issue an error. For compatibility sake we do the same thing too.
COFF_ImportHeaderType type = coff_import_header_type_from_string(exp_parse->type);
switch (type) {
case COFF_ImportHeaderType_CODE: {
B32 is_export_data = !(def->flags & (LNK_DefinedSymbolFlag_IsFunc|LNK_DefinedSymbolFlag_IsThunk));
if (is_export_data) {
lnk_error(LNK_Error_IllExport, "export \"%S\" is DATA but has specifier CODE", exp_parse->name);
}
} break;
case COFF_ImportHeaderType_DATA: {
B32 is_export_code = !!(def->flags & (LNK_DefinedSymbolFlag_IsFunc|LNK_DefinedSymbolFlag_IsThunk));
if (is_export_code) {
lnk_error(LNK_Error_IllExport, "export \"%S\" is CODE but has specifier DATA", exp_parse->name);
}
} break;
case COFF_ImportHeaderType_CONST: {
lnk_not_implemented("TODO: COFF_ImportHeaderType_CONST");
} break;
default: {
if (exp_parse->type.size) {
lnk_error(LNK_Error_IllExport, "invalid type \"%S\" for export \"%S\"", exp_parse->type, exp_parse->name);
}
} break;
}
// error check multiple def
exp = lnk_export_table_search(exptab, exp_parse->alias);
if (exp) {
if (exp->type != type) {
lnk_error(LNK_Warning_IllExport, "trying to rexport symbol \"%S\"", exp_parse->alias);
}
goto exit;
}
exp = lnk_export_table_search(exptab, exp_parse->name);
if (exp) {
if (exp->type != type) {
lnk_error(LNK_Warning_IllExport, "multiple export definition for \"%S\"", exp_parse->name);
}
goto exit;
}
// find free ordinal
U16 ordinal;
for (ordinal = 0; ordinal < exptab->max_ordinal; ++ordinal) {
if (!exptab->is_ordinal_used[ordinal]) {
exptab->is_ordinal_used[ordinal] = 1;
break;
}
}
// ordinal alloc error check
if (ordinal >= exptab->max_ordinal) {
lnk_error(LNK_Error_OutOfExportOrdinals, "reached export limit of %u, discarding export %S", exptab->max_ordinal, exp_parse->name);
goto exit;
}
// fill out export
exp = push_array_no_zero(exptab->arena, LNK_Export, 1);
exp->next = 0;
exp->name = push_str8_copy(exptab->arena, exp_parse->alias.size > 0 ? exp_parse->alias : exp_parse->name);
exp->symbol = symbol;
exp->id = exptab->name_export_list.count;
exp->ordinal = ordinal;
exp->type = type;
exp->is_private = 0; // exports through directives are public
// push node
SLLQueuePush(exptab->name_export_list.first, exptab->name_export_list.last, exp);
exptab->name_export_list.count += 1;
exit:;
return exp;
}
internal LNK_ExportArray
lnk_export_array_from_list(Arena *arena, LNK_ExportList list)
{
ProfBeginFunction();
LNK_ExportArray arr;
arr.count = 0;
arr.v = push_array_no_zero(arena, LNK_Export, list.count);
for (LNK_Export *exp = list.first; exp != NULL; exp = exp->next) {
arr.v[arr.count++] = *exp;
}
ProfEnd();
return arr;
}
internal void
lnk_build_edata(LNK_ExportTable *exptab, LNK_SectionTable *st, LNK_SymbolTable *symtab, String8 image_name, COFF_MachineType machine)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
// is export table empty?
if (exptab->name_export_list.count == 0 && exptab->noname_export_list.count == 0) {
goto exit;
}
// compute ordinal bounds
U64 ordinal_low;
for (ordinal_low = 0; ordinal_low < exptab->max_ordinal; ++ordinal_low) {
if (exptab->is_ordinal_used[ordinal_low]) {
break;
}
}
U64 ordinal_high;
for (ordinal_high = exptab->max_ordinal - 1; ordinal_high > 0; --ordinal_high) {
if (exptab->is_ordinal_used[ordinal_high]) {
break;
}
}
LNK_Section *edata = lnk_section_table_search(st, str8_lit(".edata"));
// push header
PE_ExportTable *header = push_array(edata->arena, PE_ExportTable, 1);
header->ordinal_base = safe_cast_u16(ordinal_low + 1);
header->export_address_table_count = safe_cast_u32(exptab->name_export_list.count + exptab->noname_export_list.count);
header->name_pointer_table_count = safe_cast_u32(exptab->name_export_list.count);
String8 header_data = str8((U8*)header, sizeof(*header));
String8 image_name_cstr = push_cstr(edata->arena, str8_skip_last_slash(image_name));
// push edata chunks
LNK_Chunk *header_chunk = lnk_section_push_chunk_data(edata, edata->root, header_data, str8_lit("a"));
LNK_Chunk *voff_table_chunk = lnk_section_push_chunk_list(edata, edata->root, str8_lit("b"));
LNK_Chunk *name_voff_table_chunk = lnk_section_push_chunk_list(edata, edata->root, str8_lit("c"));
LNK_Chunk *ordinal_table_chunk = lnk_section_push_chunk_list(edata, edata->root, str8_lit("d"));
LNK_Chunk *string_buffer_chunk = lnk_section_push_chunk_list(edata, edata->root, str8_lit("e"));
LNK_Chunk *image_name_chunk = lnk_section_push_chunk_data(edata, string_buffer_chunk, image_name_cstr, str8(0,0));
lnk_chunk_set_debugf(edata->arena, header_chunk, "EXPORT_HEADER");
lnk_chunk_set_debugf(edata->arena, voff_table_chunk, "EXPORT_ADDRESS_TABLE");
lnk_chunk_set_debugf(edata->arena, name_voff_table_chunk, "EXPORT_NAME_VOFF_TABLE");
lnk_chunk_set_debugf(edata->arena, ordinal_table_chunk, "EXPORT_ORDINAL_TABLE");
lnk_chunk_set_debugf(edata->arena, string_buffer_chunk, "EXPORT_STRING_BUFFER");
lnk_chunk_set_debugf(edata->arena, image_name_chunk, "EXPORT_IMAGE_NAME");
LNK_Symbol *image_name_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_lit("export_table.name_voff"), LNK_DefinedSymbolVisibility_Internal, 0, image_name_chunk, 0, 0, 0);
LNK_Symbol *address_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_lit("export_table.export_address_table_voff"), LNK_DefinedSymbolVisibility_Internal, 0, voff_table_chunk, 0, 0, 0);
LNK_Symbol *name_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_lit("export_table.name_pointer_table_voff"), LNK_DefinedSymbolVisibility_Internal, 0, name_voff_table_chunk, 0, 0, 0);
LNK_Symbol *ordinal_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_lit("export_table.ordinal_table_voff"), LNK_DefinedSymbolVisibility_Internal, 0, ordinal_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, image_name_symbol);
lnk_symbol_table_push(symtab, address_table_symbol);
lnk_symbol_table_push(symtab, name_table_symbol);
lnk_symbol_table_push(symtab, ordinal_table_symbol);
// patch header fields
lnk_section_push_reloc(edata, header_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ExportTable, name_voff), image_name_symbol);
lnk_section_push_reloc(edata, header_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ExportTable, export_address_table_voff), address_table_symbol);
lnk_section_push_reloc(edata, header_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ExportTable, name_pointer_table_voff), name_table_symbol);
lnk_section_push_reloc(edata, header_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ExportTable, ordinal_table_voff), ordinal_table_symbol);
// reserve virtual offset chunks
LNK_Chunk **ordinal_voff_map = push_array(scratch.arena, LNK_Chunk *, exptab->max_ordinal);
for (U32 i = ordinal_low; i <= ordinal_high; i += 1) {
String8 sort_index = str8_from_bits_u32(edata->arena, i);
LNK_Chunk *voff_chunk = lnk_section_push_chunk_bss(edata, voff_table_chunk, exptab->voff_size, sort_index);
ordinal_voff_map[i] = voff_chunk;
}
B8 *is_ordinal_bound = push_array(scratch.arena, B8, exptab->max_ordinal);
LNK_ExportList *exp_list_arr[] = { &exptab->name_export_list, &exptab->noname_export_list };
for (LNK_ExportList *list_ptr = exp_list_arr[0], *list_opl = list_ptr + ArrayCount(exp_list_arr);
list_ptr < list_opl;
list_ptr += 1) {
for (LNK_Export *exp = list_ptr->first; exp != 0; exp = exp->next) {
String8 name_cstr = push_cstr(edata->arena, exp->name);
// push name string
LNK_Chunk *name_chunk = lnk_section_push_chunk_data(edata, string_buffer_chunk, name_cstr, str8(0,0));
lnk_chunk_set_debugf(edata->arena, name_chunk, "export: %S", name_cstr);
// push name symbol
String8 name_export_name = push_str8f(symtab->arena, "export.%S", name_cstr);
LNK_Symbol *name_symbol = lnk_make_defined_symbol_chunk(symtab->arena, name_export_name, LNK_DefinedSymbolVisibility_Internal, 0, name_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, name_symbol);
// name voff
LNK_Chunk *voff_chunk = lnk_section_push_chunk_bss(edata, name_voff_table_chunk, exptab->voff_size, /* export table must be sorted lexically: */ name_cstr);
lnk_chunk_set_debugf(edata->arena, voff_chunk, "voff for export name %S", name_cstr);
// link reloc with name symbol
lnk_section_push_reloc(edata, voff_chunk, LNK_Reloc_VIRT_OFF_32, 0, name_symbol);
// make ordinal relative
U16 *ordinal_ptr = push_array(edata->arena, U16, 1);
*ordinal_ptr = (exp->ordinal - ordinal_low);
// ordinal
LNK_Chunk *ordinal_chunk = lnk_section_push_chunk_raw(edata, ordinal_table_chunk, ordinal_ptr, sizeof(*ordinal_ptr), /* ordinal table is parallel to the name table: */ name_cstr);
lnk_chunk_set_debugf(edata->arena, ordinal_chunk, "ordinal %u for %S", exp->ordinal, exp->name);
// (ordinal - ordinal_low) -> export virtual offset
if ( ! is_ordinal_bound[exp->ordinal]) {
is_ordinal_bound[exp->ordinal] = 1;
LNK_Chunk *export_func_voff_chunk = ordinal_voff_map[exp->ordinal];
lnk_section_push_reloc(edata, export_func_voff_chunk, LNK_Reloc_VIRT_OFF_32, 0, exp->symbol);
}
}
}
exit:;
scratch_end(scratch);
ProfEnd();
}
internal void
lnk_collect_exports_from_obj_directives(LNK_ExportTable *exptab, LNK_ObjList obj_list, LNK_SymbolTable *symtab)
{
ProfBeginFunction();
for (LNK_ObjNode *obj_node = obj_list.first; obj_node != 0; obj_node = obj_node->next) {
for (LNK_ExportParse *exp_parse = obj_node->data.export_parse.first; exp_parse != 0; exp_parse = exp_parse->next) {
lnk_export_table_push_export(exptab, symtab, exp_parse);
}
}
ProfEnd();
}
+46
View File
@@ -0,0 +1,46 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct LNK_Export
{
struct LNK_Export *next;
String8 name;
LNK_Symbol *symbol;
U32 id;
U16 ordinal;
COFF_ImportHeaderType type;
B32 is_private;
} LNK_Export;
typedef struct LNK_ExportList
{
U64 count;
LNK_Export *first;
LNK_Export *last;
} LNK_ExportList;
typedef struct LNK_ExportArray
{
U64 count;
LNK_Export *v;
} LNK_ExportArray;
typedef struct LNK_ExportTable
{
Arena *arena;
LNK_ExportList name_export_list;
LNK_ExportList noname_export_list;
U64 voff_size;
U64 max_ordinal;
B8 *is_ordinal_used;
} LNK_ExportTable;
internal LNK_ExportTable * lnk_export_table_alloc(void);
internal void lnk_export_table_release(LNK_ExportTable **exptab_ptr);
internal LNK_Export * lnk_export_table_search(LNK_ExportTable *exptab, String8 name);
internal void lnk_collect_exports_from_def_files(LNK_ExportTable *exptab, String8List path_list);
internal void lnk_build_edata(LNK_ExportTable *exptab, LNK_SectionTable *st, LNK_SymbolTable *symtab, String8 image_name, COFF_MachineType machine);
internal void lnk_collect_exports_from_obj_directives(LNK_ExportTable *exptab, LNK_ObjList obj_list, LNK_SymbolTable *symtab);
+802
View File
@@ -0,0 +1,802 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal LNK_ImportTable *
lnk_import_table_alloc_regular(LNK_SectionTable *st, LNK_SymbolTable *symtab, COFF_MachineType machine)
{
ProfBeginFunction();
LNK_Section *data_sect = lnk_section_table_push(st, str8_lit(".idata"), LNK_IDATA_SECTION_FLAGS);
LNK_Section *code_sect = lnk_section_table_search(st, str8_lit(".text"));
LNK_Chunk *dll_table_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *int_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *iat_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *ilt_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *code_chunk = lnk_section_push_chunk_list(code_sect, code_sect->root, str8_zero());
LNK_Chunk *null_dll_import = lnk_section_push_chunk_data(data_sect, dll_table_chunk, str8(0, sizeof(PE_ImportEntry)), str8_lit("zzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_dll_import, "DLL_DIRECTORY_TERMINATOR");
LNK_Symbol *dll_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_IMPORT_DLL_TABLE_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, dll_table_chunk, 0, 0, 0);
LNK_Symbol *int_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_IMPORT_NAME_TABLE_SYMBOL_NAME), LNK_DefinedSymbolVisibility_Internal, 0, int_chunk , 0, 0, 0);
LNK_Symbol *iat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_IMPORT_IAT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, iat_chunk , 0, 0, 0);
LNK_Symbol *ilt_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_IMPORT_ILT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, ilt_chunk , 0, 0, 0);
LNK_Symbol *code_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_IMPORT_JMP_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, code_chunk , 0, 0, 0);
lnk_symbol_table_push(symtab, dll_table_symbol);
lnk_symbol_table_push(symtab, int_symbol);
lnk_symbol_table_push(symtab, iat_symbol);
lnk_symbol_table_push(symtab, ilt_symbol);
lnk_symbol_table_push(symtab, code_symbol);
Arena *arena = arena_alloc();
LNK_ImportTable *imptab = push_array(arena, LNK_ImportTable, 1);
imptab->machine = machine;
imptab->arena = arena;
imptab->data_sect = data_sect;
imptab->code_sect = code_sect;
imptab->dll_table_chunk = dll_table_chunk;
imptab->int_chunk = int_chunk;
imptab->iat_chunk = iat_chunk;
imptab->ilt_chunk = ilt_chunk;
imptab->code_chunk = code_chunk;
imptab->dll_ht = hash_table_init(arena, LNK_IMPORT_DLL_HASH_TABLE_BUCKET_COUNT);
ProfEnd();
return imptab;
}
internal LNK_ImportTable *
lnk_import_table_alloc_delayed(LNK_SectionTable *st, LNK_SymbolTable *symtab, COFF_MachineType machine, B32 is_unloadable, B32 is_bindable)
{
ProfBeginFunction();
LNK_Section *data_sect = lnk_section_table_push(st, str8_lit(".didat"), LNK_DEBUG_DIR_SECTION_FLAGS);
LNK_Section *code_sect = lnk_section_table_search(st, str8_lit(".text"));
LNK_Chunk *dll_table_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *int_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *handle_table_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *iat_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *ilt_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *biat_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *uiat_chunk = lnk_section_push_chunk_list(data_sect, data_sect->root, str8_zero());
LNK_Chunk *code_chunk = lnk_section_push_chunk_list(code_sect, code_sect->root, str8_zero());
LNK_Chunk *null_dll_import = lnk_section_push_chunk_data(data_sect, dll_table_chunk, str8(0, sizeof(PE_DelayedImportEntry)), str8_lit("~0"));
lnk_chunk_set_debugf(data_sect->arena, null_dll_import, "DLL_DIRECTORY_TERMINATOR");
if (is_unloadable) {
U64 import_size = coff_word_size_from_machine(machine);
LNK_Chunk *null_uiat_chunk = lnk_section_push_chunk_bss(data_sect, uiat_chunk, import_size, str8_lit("~1"));
lnk_chunk_set_debugf(data_sect->arena, null_uiat_chunk, "UIAT_TERMINATOR");
}
if (is_bindable) {
U64 import_size = coff_word_size_from_machine(machine);
LNK_Chunk *null_biat_chunk = lnk_section_push_chunk_bss(data_sect, biat_chunk, import_size, str8_lit("~2"));
lnk_chunk_set_debugf(data_sect->arena, null_biat_chunk, "BIAT_TERMINATOR");
}
LNK_Symbol *dll_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_DLL_TABLE_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, dll_table_chunk , 0, 0, 0);
LNK_Symbol *int_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_INT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, int_chunk , 0, 0, 0);
LNK_Symbol *handle_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_HANDLE_TABLE_SYMBOL_NAME), LNK_DefinedSymbolVisibility_Internal, 0, handle_table_chunk, 0, 0, 0);
LNK_Symbol *iat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_IAT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, iat_chunk , 0, 0, 0);
LNK_Symbol *ilt_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_ILT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, ilt_chunk , 0, 0, 0);
LNK_Symbol *biat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_BIAT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, biat_chunk , 0, 0, 0);
LNK_Symbol *uiat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_UIAT_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, uiat_chunk , 0, 0, 0);
LNK_Symbol *code_symbol = lnk_make_defined_symbol_chunk(symtab->arena, str8_cstring(LNK_DELAYED_IMPORT_CODE_SYMBOL_NAME) , LNK_DefinedSymbolVisibility_Internal, 0, code_chunk , 0, 0, 0);
lnk_symbol_table_push(symtab, dll_table_symbol);
lnk_symbol_table_push(symtab, int_symbol);
lnk_symbol_table_push(symtab, handle_table_symbol);
lnk_symbol_table_push(symtab, iat_symbol);
lnk_symbol_table_push(symtab, ilt_symbol);
lnk_symbol_table_push(symtab, biat_symbol);
lnk_symbol_table_push(symtab, uiat_symbol);
lnk_symbol_table_push(symtab, code_symbol);
LNK_ImportTableFlags flags = 0;
if (is_unloadable) {
flags |= LNK_ImportTableFlag_EmitUiat;
}
if (is_bindable) {
flags |= LNK_ImportTableFlag_EmitBiat;
}
Arena *arena = arena_alloc();
LNK_ImportTable *imptab = push_array(arena, LNK_ImportTable, 1);
imptab->arena = arena;
imptab->machine = machine;
imptab->data_sect = data_sect;
imptab->code_sect = code_sect;
imptab->dll_table_chunk = dll_table_chunk;
imptab->int_chunk = int_chunk;
imptab->handle_table_chunk = handle_table_chunk;
imptab->iat_chunk = iat_chunk;
imptab->ilt_chunk = ilt_chunk;
imptab->biat_chunk = biat_chunk;
imptab->uiat_chunk = uiat_chunk;
imptab->code_chunk = code_chunk;
imptab->flags = flags;
imptab->dll_ht = hash_table_init(arena, LNK_IMPORT_FUNC_HASH_TABLE_BUCKET_COUNT);
ProfEnd();
return imptab;
}
internal void
lnk_import_table_release(LNK_ImportTable **imptab_ptr)
{
ProfBeginFunction();
arena_release((*imptab_ptr)->arena);
*imptab_ptr = 0;
ProfEnd();
}
internal BucketNode *
lnk_import_table_push_dll_node(LNK_ImportTable *imptab, LNK_ImportDLL *dll)
{
// update list
SLLQueuePush(imptab->first_dll, imptab->last_dll, dll);
// update name -> dll hash table
return hash_table_push_path_raw(imptab->arena, imptab->dll_ht, dll->name, dll);
}
internal BucketNode *
lnk_import_table_push_func_node(LNK_ImportTable *imptab, LNK_ImportDLL *dll, LNK_ImportFunc *func)
{
// update list
SLLQueuePush(dll->first_func, dll->last_func, func);
// update name -> func hash table
return hash_table_push_string_raw(imptab->arena, dll->func_ht, func->name, func);
}
internal LNK_ImportDLL *
lnk_import_table_search_dll(LNK_ImportTable *imptab, String8 name)
{
KeyValuePair *kv = hash_table_search_path(imptab->dll_ht, name);
if (kv) {
Assert(kv->value_raw);
return kv->value_raw;
}
return 0;
}
internal LNK_ImportFunc *
lnk_import_table_search_func(LNK_ImportDLL *dll, String8 name)
{
KeyValuePair *kv = hash_table_search_string(dll->func_ht, name);
if (kv) {
Assert(kv->value_raw);
return kv->value_raw;
}
return 0;
}
internal LNK_ImportDLL *
lnk_import_table_push_dll_regular(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, String8 dll_name, COFF_MachineType machine)
{
ProfBeginFunction();
// TODO: error handle
Assert(imptab->machine == machine);
LNK_Section *data_sect = imptab->data_sect;
LNK_Section *code_sect = imptab->code_sect;
LNK_Chunk *int_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->int_chunk, str8_zero());
LNK_Chunk *ilt_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->ilt_chunk, str8_zero());
LNK_Chunk *iat_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->iat_chunk, str8_zero());
LNK_Chunk *code_table_chunk = lnk_section_push_chunk_list(code_sect, imptab->code_chunk, str8_zero());
String8 ilt_symbol_name = push_str8f(symtab->arena, "%S.lookup_table_voff", dll_name);
LNK_Symbol *ilt_symbol = lnk_make_defined_symbol_chunk(symtab->arena, ilt_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, ilt_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, ilt_symbol);
String8 iat_symbol_name = push_str8f(symtab->arena, "%S.import_addr_table_voff", dll_name);
LNK_Symbol *iat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, iat_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, iat_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, iat_symbol);
String8 dll_name_cstr = push_cstr(data_sect->arena, dll_name);
LNK_Chunk *dll_name_chunk = lnk_section_push_chunk_data(data_sect, int_table_chunk, dll_name_cstr, str8_zero());
lnk_chunk_set_debugf(data_sect->arena, dll_name_chunk, "DLL name chunk (%S)", dll_name);
String8 dll_name_voff_name = push_str8f(symtab->arena, "%S.name_voff", dll_name);
LNK_Symbol *dll_name_voff_symbol = lnk_make_defined_symbol_chunk(symtab->arena, dll_name_voff_name, LNK_DefinedSymbolVisibility_Internal, 0, dll_name_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, dll_name_voff_symbol);
// chunk for dll directory entry
PE_ImportEntry *dir = push_array(imptab->arena, PE_ImportEntry, 1);
LNK_Chunk *dll_chunk = lnk_section_push_chunk_data(data_sect, imptab->dll_table_chunk, str8_struct(dir), str8_zero());
lnk_chunk_set_debugf(data_sect->arena, dll_chunk, "DLL Directory for %S", dll_name);
// patch dll import fields
lnk_section_push_reloc(data_sect, dll_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ImportEntry, lookup_table_voff), ilt_symbol);
lnk_section_push_reloc(data_sect, dll_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ImportEntry, name_voff), dll_name_voff_symbol);
lnk_section_push_reloc(data_sect, dll_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_ImportEntry, import_addr_table_voff), iat_symbol);
U64 import_size = coff_word_size_from_machine(machine);
// null entry to terminate import lookup table array
LNK_Chunk *null_ilt_chunk = lnk_section_push_chunk_data(data_sect, ilt_table_chunk, str8(0, import_size), str8_lit("zzzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_ilt_chunk, "%S: ILT terminator", dll_name);
// null entry to terminate import address table array
LNK_Chunk *null_iat_chunk = lnk_section_push_chunk_data(data_sect, iat_table_chunk, str8(0, import_size), str8_lit("zzzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_iat_chunk, "%S: IAT terminator", dll_name);
// push to list
LNK_ImportDLL *dll = push_array(imptab->arena, LNK_ImportDLL, 1);
dll->name = push_str8_copy(imptab->arena, dll_name);
dll->dll_chunk = dll_chunk;
dll->int_table_chunk = int_table_chunk;
dll->ilt_table_chunk = ilt_table_chunk;
dll->iat_table_chunk = iat_table_chunk;
dll->code_table_chunk = code_table_chunk;
dll->machine = machine;
dll->func_ht = hash_table_init(imptab->arena, LNK_IMPORT_FUNC_HASH_TABLE_BUCKET_COUNT);
lnk_import_table_push_dll_node(imptab, dll);
ProfEnd();
return dll;
}
internal LNK_ImportDLL *
lnk_import_table_push_dll_delayed(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, String8 dll_name, COFF_MachineType machine)
{
ProfBeginFunction();
Assert(imptab->machine == machine);
U64 handle_size = coff_word_size_from_machine(machine);
U64 import_size = coff_word_size_from_machine(machine);
// shortcuts
LNK_Section *data_sect = imptab->data_sect;
LNK_Section *code_sect = imptab->code_sect;
// init DLL entry
PE_DelayedImportEntry *imp_desc = push_array(data_sect->arena, PE_DelayedImportEntry, 1);
imp_desc->attributes = 1;
imp_desc->name_voff = 0; // relocated
imp_desc->module_handle_voff = 0; // relocated
imp_desc->iat_voff = 0; // relocated
imp_desc->name_table_voff = 0; // relocated
imp_desc->bound_table_voff = 0; // relocated
imp_desc->unload_table_voff = 0; // relocated
imp_desc->time_stamp = 0;
// emit entry chunk
String8 imp_desc_data = str8_struct(imp_desc);
LNK_Chunk *imp_desc_chunk = lnk_section_push_chunk_data(data_sect, imptab->dll_table_chunk, imp_desc_data, str8_zero());
// emit entry symbol
String8 imp_desc_name = push_str8f(symtab->arena, "__DELAY_IMPORT_DESCRIPTOR_%S", dll_name);
LNK_Symbol *imp_desc_symbol = lnk_make_defined_symbol_chunk(symtab->arena, imp_desc_name, LNK_DefinedSymbolVisibility_Extern, 0, imp_desc_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, imp_desc_symbol);
// emit string table chunk
String8 int_table_chunk_debug = push_str8f(data_sect->arena, "delayed.%S.int", dll_name);
LNK_Chunk *int_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->int_chunk, int_table_chunk_debug);
String8 int_table_symbol_name = push_str8f(symtab->arena, "delayed.%S.int", dll_name);
LNK_Symbol *int_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, int_table_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, int_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, int_table_symbol);
LNK_Chunk *null_string_chunk = lnk_section_push_chunk_list(data_sect, int_table_chunk, str8_lit("zzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_string_chunk, "string table null");
// emit DLL name chunk
String8 name_chunk_data = push_cstr(data_sect->arena, dll_name);
LNK_Chunk *name_chunk = lnk_section_push_chunk_data(data_sect, int_table_chunk, name_chunk_data, str8_zero());
String8 name_symbol_name = push_str8f(symtab->arena, "delayed.%S.name", dll_name);
LNK_Symbol *name_symbol = lnk_make_defined_symbol_chunk(symtab->arena, name_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, name_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, name_symbol);
// patch DLL name voff
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, name_voff), name_symbol);
// emit DLL handle chunk
LNK_Chunk *handle_chunk = lnk_section_push_chunk_bss(data_sect, imptab->handle_table_chunk, handle_size, str8_zero());
String8 handle_name = push_str8f(symtab->arena, "delayed.%S.handle", dll_name);
LNK_Symbol *handle_symbol = lnk_make_defined_symbol_chunk(symtab->arena, handle_name, LNK_DefinedSymbolVisibility_Internal, 0, handle_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, handle_symbol);
// patch DLL handle voff
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, module_handle_voff), handle_symbol);
// emit IAT chunk
LNK_Chunk *iat_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->iat_chunk, str8_zero());
String8 iat_table_name = push_str8f(symtab->arena, "delayed.%S.iat", dll_name);
LNK_Symbol *iat_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, iat_table_name, LNK_DefinedSymbolVisibility_Internal, 0, iat_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, iat_table_symbol);
LNK_Chunk *null_iat_chunk = lnk_section_push_chunk_bss(data_sect, iat_table_chunk, import_size, str8_lit("zzzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_iat_chunk, "%S: IAT terminator", dll_name);
// emit ILT chunk
LNK_Chunk *ilt_table_chunk = lnk_section_push_chunk_list(data_sect, imptab->ilt_chunk, str8_zero());
LNK_Chunk *null_ilt_chunk = lnk_section_push_chunk_bss(data_sect, ilt_table_chunk, import_size, str8_lit("zzzzzz"));
lnk_chunk_set_debugf(data_sect->arena, null_ilt_chunk, "%S: ILT terminator", dll_name);
String8 ilt_table_name = push_str8f(symtab->arena, "delayed.%S.ilt", dll_name);
LNK_Symbol *ilt_table_symbol = lnk_make_defined_symbol_chunk(symtab->arena, ilt_table_name, LNK_DefinedSymbolVisibility_Extern, 0, ilt_table_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, ilt_table_symbol);
// patch import address table voff
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, iat_voff), iat_table_symbol);
// patch string table voff
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, name_table_voff), ilt_table_symbol);
// emit bound table chunk
LNK_Chunk *biat_chunk = 0;
if (imptab->flags & LNK_ImportTableFlag_EmitBiat) {
biat_chunk = lnk_section_push_chunk_list(data_sect, imptab->biat_chunk, str8_zero());
String8 biat_symbol_name = push_str8f(symtab->arena, "delayed.%S.BIAT", dll_name);
LNK_Symbol *biat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, biat_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, biat_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, biat_symbol);
// patch BIAT field off
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, bound_table_voff), biat_symbol);
}
// emit unload table chunk
LNK_Chunk *uiat_chunk = NULL;
if (imptab->flags & LNK_ImportTableFlag_EmitUiat) {
uiat_chunk = lnk_section_push_chunk_list(data_sect, imptab->uiat_chunk, str8_zero());
String8 uiat_symbol_name = push_str8f(symtab->arena, "delayed.%S.UIAT", dll_name);
LNK_Symbol *uiat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, uiat_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, uiat_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, uiat_symbol);
// patch UIAT field voff
lnk_section_push_reloc(data_sect, imp_desc_chunk, LNK_Reloc_VIRT_OFF_32, OffsetOf(PE_DelayedImportEntry, unload_table_voff), uiat_symbol);
}
// emit chunk for DLL thunk/load code
LNK_Chunk *code_chunk = lnk_section_push_chunk_list(code_sect, imptab->code_chunk, str8_zero());
lnk_chunk_set_debugf(code_sect->arena, code_chunk, "code for %S", dll_name);
// emit tail merge
LNK_Chunk *tail_merge_chunk = 0;
switch (machine) {
case COFF_MachineType_X64: {
LNK_Symbol *delay_load_helper_symbol = lnk_make_undefined_symbol(symtab->arena, str8_lit(LNK_DELAY_LOAD_HELPER2_SYMBOL_NAME), LNK_SymbolScopeFlag_Main);
tail_merge_chunk = lnk_emit_tail_merge_thunk_x64(code_sect, code_chunk, imp_desc_symbol, delay_load_helper_symbol);
} break;
default: {
lnk_not_implemented("TODO: __tailMerge for %S", coff_string_from_machine_type(machine));
} break;
}
// fill out result
LNK_ImportDLL *dll = push_array(imptab->arena, LNK_ImportDLL, 1);
dll->dll_chunk = imp_desc_chunk;
dll->int_table_chunk = int_table_chunk;
dll->iat_table_chunk = iat_table_chunk;
dll->ilt_table_chunk = ilt_table_chunk;
dll->biat_table_chunk = biat_chunk;
dll->uiat_table_chunk = uiat_chunk;
dll->code_table_chunk = code_chunk;
dll->tail_merge_symbol = lnk_emit_tail_merge_symbol(symtab, tail_merge_chunk, dll_name);
dll->name = push_str8_copy(imptab->arena, dll_name);
dll->machine = machine;
dll->func_ht = hash_table_init(imptab->arena, LNK_IMPORT_FUNC_HASH_TABLE_BUCKET_COUNT);
lnk_import_table_push_dll_node(imptab, dll);
ProfEnd();
return dll;
}
internal LNK_ImportFunc *
lnk_import_table_push_func_regular(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, LNK_ImportDLL *dll, COFF_ImportHeader *header)
{
ProfBeginFunction();
Assert(header->machine == dll->machine); // TODO: error handling
LNK_Section *data_sect = imptab->data_sect;
LNK_Section *code_sect = imptab->code_sect;
LNK_Chunk *int_table_chunk = dll->int_table_chunk;
LNK_Chunk *ilt_table_chunk = dll->ilt_table_chunk;
LNK_Chunk *iat_table_chunk = dll->iat_table_chunk;
LNK_Chunk *code_table_chunk = dll->code_table_chunk;
LNK_Chunk *ilt_chunk = g_null_chunk_ptr;
LNK_Chunk *iat_chunk = g_null_chunk_ptr;
U64 import_size = coff_word_size_from_machine(dll->machine);
// generate sort index (optional)
String8 sort_index = str8_from_bits_u32(data_sect->arena, header->hint);
switch (header->name_type) {
case COFF_ImportHeaderNameType_ORDINAL: {
String8 ordinal_data = lnk_ordinal_data_from_hint(data_sect->arena, dll->machine, header->hint);
ilt_chunk = lnk_section_push_chunk_data(data_sect, ilt_table_chunk, ordinal_data, sort_index);
iat_chunk = lnk_section_push_chunk_data(data_sect, iat_table_chunk, ordinal_data, sort_index);
// associate chunks
lnk_section_associate_chunks(data_sect, iat_chunk, ilt_chunk);
} break;
case COFF_ImportHeaderNameType_NAME: {
// put together name look up entry
String8 int_data = coff_make_import_lookup(data_sect->arena, header->hint, header->func_name);
LNK_Chunk *int_chunk = lnk_section_push_chunk_data(data_sect, int_table_chunk, int_data, str8_zero());
// create symbol for lookup chunk
String8 int_symbol_name = push_str8f(symtab->arena, "regular.%S.%S.name", dll->name, header->func_name);
LNK_Symbol *int_symbol = lnk_make_defined_symbol_chunk(symtab->arena, int_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, int_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, int_symbol);
// in the file IAT mirrors ILT, dynamic linker later overwrites it with imported function addresses.
ilt_chunk = lnk_section_push_chunk_bss(data_sect, ilt_table_chunk, import_size, sort_index);
iat_chunk = lnk_section_push_chunk_bss(data_sect, iat_table_chunk, import_size, sort_index);
lnk_chunk_set_debugf(data_sect->arena, ilt_chunk, "ILT entry for %S.%S", dll->name, header->func_name);
lnk_chunk_set_debugf(data_sect->arena, iat_chunk, "IAT entry for %S.%S", dll->name, header->func_name);
// associate chunks
lnk_section_associate_chunks(data_sect, iat_chunk, ilt_chunk);
lnk_section_associate_chunks(data_sect, iat_chunk, int_chunk);
// patch IAT and ILT
lnk_section_push_reloc(data_sect, ilt_chunk, LNK_Reloc_VIRT_OFF_32, 0, int_symbol);
lnk_section_push_reloc(data_sect, iat_chunk, LNK_Reloc_VIRT_OFF_32, 0, int_symbol);
} break;
case COFF_ImportHeaderNameType_UNDECORATE: {
lnk_not_implemented("TODO: COFF_ImportHeaderNameType_UNDECORATE");
} break;
case COFF_ImportHeaderNameType_NAME_NOPREFIX: {
lnk_not_implemented("TODO: COFF_ImportHeaderNameType_NAME_NOPREFIX");
} break;
}
String8 ilt_symbol_name = push_str8f(symtab->arena, "regular.%S.%S.ilt", dll->name, header->func_name);
String8 iat_symbol_name = push_str8f(symtab->arena, "__imp_%S", header->func_name);
LNK_Symbol *ilt_symbol = lnk_make_defined_symbol_chunk(symtab->arena, ilt_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, ilt_chunk, 0, 0, 0);
LNK_Symbol *iat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, iat_symbol_name, LNK_DefinedSymbolVisibility_Extern, 0, iat_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, ilt_symbol);
lnk_symbol_table_push(symtab, iat_symbol);
// generate thunks
LNK_Symbol *jmp_thunk_symbol = g_null_symbol_ptr;
if (header->type == COFF_ImportHeaderType_CODE) {
switch (dll->machine) {
case COFF_MachineType_X64: {
// generate jump thunk
LNK_Chunk *jmp_thunk_chunk = lnk_emit_indirect_jump_thunk_x64(code_sect, code_table_chunk, iat_symbol);
lnk_section_associate_chunks(data_sect, iat_chunk, jmp_thunk_chunk);
// push jump thunk symbol
String8 jmp_thunk_symbol_name = push_str8_copy(symtab->arena, header->func_name);
jmp_thunk_symbol = lnk_emit_jmp_thunk_symbol(symtab, jmp_thunk_chunk, jmp_thunk_symbol_name);
lnk_symbol_set_debugf(symtab->arena, jmp_thunk_symbol, "x64 jmp thunk %S.%S", dll->name, header->func_name);
} break;
default: lnk_not_implemented("TODO: support for machine 0x%X", dll->machine); break;
}
}
// fill out import
LNK_ImportFunc *func = push_array(imptab->arena, LNK_ImportFunc, 1);
func->name = push_str8_copy(imptab->arena, header->func_name);
func->thunk_symbol_name = push_str8_copy(imptab->arena, jmp_thunk_symbol->name);
func->iat_symbol_name = push_str8_copy(imptab->arena, iat_symbol->name);
lnk_import_table_push_func_node(imptab, dll, func);
ProfEnd();
return func;
}
internal LNK_ImportFunc *
lnk_import_table_push_func_delayed(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, LNK_ImportDLL *dll, COFF_ImportHeader *header)
{
ProfBeginFunction();
Assert(dll->machine == header->machine); // TODO: error handle
U64 import_size = coff_word_size_from_machine(dll->machine);
LNK_Section *data_sect = imptab->data_sect;
LNK_Section *code_sect = imptab->code_sect;
LNK_Chunk *int_table_chunk = dll->int_table_chunk;
LNK_Chunk *ilt_table_chunk = dll->ilt_table_chunk;
LNK_Chunk *iat_table_chunk = dll->iat_table_chunk;
LNK_Chunk *biat_table_chunk = dll->biat_table_chunk;
LNK_Chunk *uiat_table_chunk = dll->uiat_table_chunk;
LNK_Chunk *code_table_chunk = dll->code_table_chunk;
LNK_Chunk *ilt_chunk = g_null_chunk_ptr;
LNK_Chunk *iat_chunk = g_null_chunk_ptr;
LNK_Chunk *uiat_chunk = g_null_chunk_ptr;
LNK_Chunk *biat_chunk = g_null_chunk_ptr;
LNK_Symbol *int_symbol = 0;
// generate sort index (optional)
String8 sort_index = str8_from_bits_u32(data_sect->arena, header->hint);
// generate thunks
LNK_Symbol *jmp_thunk_symbol = g_null_symbol_ptr;
LNK_Symbol *load_thunk_symbol = g_null_symbol_ptr;
LNK_Chunk *jmp_thunk_chunk = 0;
LNK_Chunk *load_thunk_chunk = 0;
if (header->type == COFF_ImportHeaderType_CODE) {
switch (dll->machine) {
case COFF_MachineType_X64: {
String8 iat_symbol_name = push_str8f(symtab->arena, "__imp_%S", header->func_name);
LNK_Symbol *iat_symbol = lnk_make_undefined_symbol(symtab->arena, iat_symbol_name, LNK_SymbolScopeFlag_Main);
lnk_symbol_set_debugf(symtab->arena, iat_symbol, "x64 IAT (delayed) %S.%S", dll->name, header->func_name);
// emit jmp thunk chunk
jmp_thunk_chunk = lnk_emit_indirect_jump_thunk_x64(code_sect, code_table_chunk, iat_symbol);
jmp_thunk_symbol = lnk_emit_jmp_thunk_symbol(symtab, jmp_thunk_chunk, header->func_name);
lnk_symbol_set_debugf(symtab->arena, jmp_thunk_symbol, "x64 jmp thunk (delayed) %S.%S", dll->name, header->func_name);
// emit load thunk
load_thunk_chunk = lnk_emit_load_thunk_x64(code_sect, code_table_chunk, iat_symbol, dll->tail_merge_symbol);
load_thunk_symbol = lnk_emit_load_thunk_symbol(symtab, load_thunk_chunk, header->func_name);
lnk_symbol_set_debugf(symtab->arena, load_thunk_symbol, "x64 load thunk (delayed) %S.%S", dll->name, header->func_name);
} break;
default: lnk_not_implemented("TODO: support for machine 0x%X", dll->machine); break;
}
}
switch (header->name_type) {
case COFF_ImportHeaderNameType_ORDINAL: {
String8 ordinal_data = lnk_ordinal_data_from_hint(data_sect->arena, dll->machine, header->hint);
Assert(ordinal_data.size == import_size);
ilt_chunk = lnk_section_push_chunk_data(data_sect, ilt_table_chunk, ordinal_data, sort_index);
iat_chunk = lnk_section_push_chunk_bss(data_sect, iat_table_chunk, import_size, sort_index);
lnk_section_push_reloc(data_sect, iat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
lnk_section_associate_chunks(data_sect, iat_chunk, ilt_chunk);
if (imptab->flags & LNK_ImportTableFlag_EmitBiat) {
biat_chunk = lnk_section_push_chunk_bss(data_sect, biat_table_chunk, import_size, sort_index);
lnk_section_push_reloc(data_sect, biat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
lnk_section_associate_chunks(data_sect, iat_chunk, biat_chunk);
}
if (imptab->flags & LNK_ImportTableFlag_EmitUiat) {
uiat_chunk = lnk_section_push_chunk_bss(data_sect, uiat_table_chunk, import_size, sort_index);
lnk_section_push_reloc(data_sect, uiat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
lnk_section_associate_chunks(data_sect, iat_chunk, uiat_chunk);
}
} break;
case COFF_ImportHeaderNameType_NAME: {
// put together name look up entry
String8 int_data = coff_make_import_lookup(data_sect->arena, header->hint, header->func_name);
LNK_Chunk *int_chunk = lnk_section_push_chunk_data(data_sect, int_table_chunk, int_data, str8_zero());
// create symbol for lookup chunk
String8 int_symbol_name = push_str8f(symtab->arena, "%S.%S.name.delayed", dll->name, header->func_name);
int_symbol = lnk_make_defined_symbol_chunk(symtab->arena, int_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, int_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, int_symbol);
// dynamic linker patches this voff on DLL load event
ilt_chunk = lnk_section_push_chunk_bss(data_sect, ilt_table_chunk, import_size, sort_index);
lnk_chunk_set_debugf(data_sect->arena, ilt_chunk, "ILT entry (delayed) %S.%S", dll->name, header->func_name);
// patch-in ILT with import voff
lnk_section_push_reloc(data_sect, ilt_chunk, LNK_Reloc_VIRT_OFF_32, 0, int_symbol);
// in the file IAT mirrors ILT, dynamic linker later overwrites it with imported function addresses.
iat_chunk = lnk_section_push_chunk_bss(data_sect, iat_table_chunk, import_size, sort_index);
lnk_chunk_set_debugf(data_sect->arena, iat_chunk, "IAT entre (delayed) %S.%S", dll->name, header->func_name);
// associate chunks
lnk_section_associate_chunks(data_sect, iat_chunk, ilt_chunk);
lnk_section_associate_chunks(data_sect, iat_chunk, int_chunk);
// patch-in thunk address
lnk_section_push_reloc(data_sect, iat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
if (imptab->flags & LNK_ImportTableFlag_EmitBiat) {
biat_chunk = lnk_section_push_chunk_bss(data_sect, biat_table_chunk, import_size, sort_index);
lnk_chunk_set_debugf(data_sect->arena, biat_chunk, "%S.biat.%S (delayed)", dll->name, header->func_name);
// patch-in thunk address
lnk_section_push_reloc(data_sect, biat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
}
if (imptab->flags & LNK_ImportTableFlag_EmitUiat) {
uiat_chunk = lnk_section_push_chunk_bss(data_sect, uiat_table_chunk, import_size, sort_index);
lnk_chunk_set_debugf(data_sect->arena, uiat_chunk, "%S.uiat.%S (delayed)", dll->name, header->func_name);
// patch-in thunk address
lnk_section_push_reloc(data_sect, uiat_chunk, LNK_Reloc_ADDR_64, 0, load_thunk_symbol);
}
} break;
case COFF_ImportHeaderNameType_UNDECORATE: {
lnk_not_implemented("TODO: COFF_ImportHeaderNameType_UNDECORATE");
} break;
case COFF_ImportHeaderNameType_NAME_NOPREFIX: {
lnk_not_implemented("TODO: COFF_ImportHeaderNameType_NAME_NOPREFIX");
} break;
}
if (jmp_thunk_chunk) {
lnk_section_associate_chunks(data_sect, iat_chunk, jmp_thunk_chunk);
}
if (load_thunk_chunk) {
lnk_section_associate_chunks(data_sect, iat_chunk, load_thunk_chunk);
}
String8 iat_symbol_name = push_str8f(symtab->arena, "__imp_%S", header->func_name);
LNK_Symbol *iat_symbol = lnk_make_defined_symbol_chunk(symtab->arena, iat_symbol_name, LNK_DefinedSymbolVisibility_Extern, 0, iat_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, iat_symbol);
String8 ilt_symbol_name = push_str8f(symtab->arena, "%S.%S.ilt.delayed", dll->name, header->func_name);
LNK_Symbol *ilt_symbol = lnk_make_defined_symbol_chunk(symtab->arena, ilt_symbol_name, LNK_DefinedSymbolVisibility_Internal, 0, ilt_chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, ilt_symbol);
// fill out import
LNK_ImportFunc *func = push_array(imptab->arena, LNK_ImportFunc, 1);
func->name = push_str8_copy(imptab->arena, header->func_name);
func->thunk_symbol_name = push_str8_copy(imptab->arena, jmp_thunk_symbol->name);
func->iat_symbol_name = push_str8_copy(imptab->arena, iat_symbol->name);
lnk_import_table_push_func_node(imptab, dll, func);
ProfEnd();
return func;
}
internal String8
lnk_ordinal_data_from_hint(Arena *arena, COFF_MachineType machine, U16 hint)
{
String8 ordinal_data = str8_zero();
switch (machine) {
case COFF_MachineType_X64: {
U64 *ordinal = push_array(arena, U64, 1);
*ordinal = coff_make_ordinal_64(hint);
ordinal_data = str8_struct(ordinal);
} break;
case COFF_MachineType_X86: {
U32 *ordinal = push_array(arena, U32, 1);
*ordinal = coff_make_ordinal_32(hint);
ordinal_data = str8_struct(ordinal);
} break;
default: lnk_not_implemented("TODO: support for machine 0x%x", machine);
}
return ordinal_data;
}
internal LNK_Chunk *
lnk_emit_indirect_jump_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *addr_ptr)
{
ProfBeginFunction();
static U8 thunk[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 }; // jmp [__imp_<FUNC_NAME>]
// emit chunk
String8 jmp_data = push_str8_copy(sect->arena, str8_array_fixed(thunk));
LNK_Chunk *jmp_chunk = lnk_section_push_chunk_data(sect, parent, jmp_data, str8_zero());
// patch thunk with imports address
static const U64 JMP_OPERAND_OFFSET = 2;
lnk_section_push_reloc(sect, jmp_chunk, LNK_Reloc_REL32, JMP_OPERAND_OFFSET, addr_ptr);
ProfEnd();
return jmp_chunk;
}
internal LNK_Chunk *
lnk_emit_load_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *imp_addr_ptr, LNK_Symbol *tail_merge)
{
ProfBeginFunction();
static U8 load_thunk[] = {
0x48, 0x8D, 0x05, 0x00, 0x00, 0x00, 0x00, // lea rax, [__imp_<FUNC_NAME>]
0xE9, 0x00, 0x00, 0x00, 0x00 // jmp __tailMerge_<DLL_NAME>
};
// emit load thunk chunk
String8 load_thunk_data = push_str8_copy(sect->arena, str8_array_fixed(load_thunk));
LNK_Chunk *load_thunk_chunk = lnk_section_push_chunk_data(sect, parent, load_thunk_data, str8_zero());
// patch lea with IAT entry
static const U64 LEA_OPERAND_OFFSET = 3;
lnk_section_push_reloc(sect, load_thunk_chunk, LNK_Reloc_REL32, LEA_OPERAND_OFFSET, imp_addr_ptr);
// patch jmp __tailMerge_<DLL_NAME>
static const U64 JMP_OPERAND_OFFSET = 8;
lnk_section_push_reloc(sect, load_thunk_chunk, LNK_Reloc_REL32, JMP_OPERAND_OFFSET, tail_merge);
ProfEnd();
return load_thunk_chunk;
}
internal LNK_Chunk *
lnk_emit_tail_merge_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *dll_import_descriptor, LNK_Symbol *delay_load_helper)
{
ProfBeginFunction();
static U8 tail_merge[] = {
0x48, 0x89, 0x4C, 0x24, 0x08, // mov qword ptr [rsp+8],rcx
0x48, 0x89, 0x54, 0x24, 0x10, // mov qword ptr [rsp+10h],rdx
0x4C, 0x89, 0x44, 0x24, 0x18, // mov qword ptr [rsp+18h],r8
0x4C, 0x89, 0x4C, 0x24, 0x20, // mov qword ptr [rsp+20h],r9
0x48, 0x83, 0xEC, 0x68, // sub rsp,68h
0x66, 0x0F, 0x7F, 0x44, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h],xmm0
0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h],xmm1
0x66, 0x0F, 0x7F, 0x54, 0x24, 0x40, // movdqa xmmword ptr [rsp+40h],xmm2
0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x50, // movdqa xmmword ptr [rsp+50h],xmm3
0x48, 0x8B, 0xD0, // mov rdx,rax
0x48, 0x8D, 0x0D, 0x00, 0x00, 0x00, 0x00, // lea rcx,[__DELAY_IMPORT_DESCRIPTOR_<DLL_NAME>]
0xE8, 0x00, 0x00, 0x00, 0x00, // call __delayLoadHelper2
0x66, 0x0F, 0x6F, 0x44, 0x24, 0x20, // movdqa xmm0,xmmword ptr [rsp+20h]
0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x30, // movdqa xmm1,xmmword ptr [rsp+30h]
0x66, 0x0F, 0x6F, 0x54, 0x24, 0x40, // movdqa xmm2,xmmword ptr [rsp+40h]
0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x50, // movdqa xmm3,xmmword ptr [rsp+50h]
0x48, 0x8B, 0x4C, 0x24, 0x70, // mov rcx,qword ptr [rsp+70h]
0x48, 0x8B, 0x54, 0x24, 0x78, // mov rdx,qword ptr [rsp+78h]
0x4C, 0x8B, 0x84, 0x24, 0x80, 0x00, 0x00, 0x00, // mov r8,qword ptr [rsp+80h]
0x4C, 0x8B, 0x8C, 0x24, 0x88, 0x00, 0x00, 0x00, // mov r9,qword ptr [rsp+88h]
0x48, 0x83, 0xC4, 0x68, // add rsp,68h
0xFF, 0xE0, // jmp rax
};
// emit tail merge chunk
String8 tail_merge_data = push_str8_copy(sect->arena, str8_array_fixed(tail_merge));
LNK_Chunk *tail_merge_chunk = lnk_section_push_chunk_data(sect, parent, tail_merge_data, str8_zero());
// patch lea __DELAY_IMPORT_DESCRIPTOR_<DLL_NAME>
static const U64 LEA_OPERAND_OFFSET = 54;
lnk_section_push_reloc(sect, tail_merge_chunk, LNK_Reloc_REL32, LEA_OPERAND_OFFSET, dll_import_descriptor);
// patch call __delayLoadHelper2
static const U64 CALL_OPERAND_OFFSET = 59;
lnk_section_push_reloc(sect, tail_merge_chunk, LNK_Reloc_REL32, CALL_OPERAND_OFFSET, delay_load_helper);
ProfEnd();
return tail_merge_chunk;
}
internal LNK_Symbol *
lnk_emit_load_thunk_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name)
{
ProfBeginFunction();
// emit load thunk symbol
String8 load_thunk_name = push_str8f(symtab->arena, "__imp_load_%S", func_name);
LNK_Symbol *load_thunk_symbol = lnk_make_defined_symbol_chunk(symtab->arena, load_thunk_name, LNK_DefinedSymbolVisibility_Extern, LNK_DefinedSymbolFlag_IsFunc|LNK_DefinedSymbolFlag_IsThunk, chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, load_thunk_symbol);
ProfEnd();
return load_thunk_symbol;
}
internal LNK_Symbol *
lnk_emit_jmp_thunk_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name)
{
ProfBeginFunction();
String8 jmp_thunk_name = push_str8f(symtab->arena, "%S", func_name);
LNK_Symbol *jmp_thunk_symbol = lnk_make_defined_symbol_chunk(symtab->arena, jmp_thunk_name, LNK_DefinedSymbolVisibility_Extern, LNK_DefinedSymbolFlag_IsFunc|LNK_DefinedSymbolFlag_IsThunk, chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, jmp_thunk_symbol);
ProfEnd();
return jmp_thunk_symbol;
}
internal LNK_Symbol *
lnk_emit_tail_merge_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name)
{
ProfBeginFunction();
String8 tail_merge_name = push_str8f(symtab->arena, "__tailMerge_%S", func_name);
LNK_Symbol *tail_merge_symbol = lnk_make_defined_symbol_chunk(symtab->arena, tail_merge_name, LNK_DefinedSymbolVisibility_Extern, LNK_DefinedSymbolFlag_IsFunc|LNK_DefinedSymbolFlag_IsThunk, chunk, 0, 0, 0);
lnk_symbol_table_push(symtab, tail_merge_symbol);
ProfEnd();
return tail_merge_symbol;
}
+81
View File
@@ -0,0 +1,81 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#define LNK_IMPORT_DLL_HASH_TABLE_BUCKET_COUNT 512
#define LNK_IMPORT_FUNC_HASH_TABLE_BUCKET_COUNT 2048
typedef struct LNK_ImportFunc
{
struct LNK_ImportFunc *next;
String8 name;
String8 thunk_symbol_name;
String8 iat_symbol_name;
} LNK_ImportFunc;
typedef struct LNK_ImportDLL
{
struct LNK_ImportDLL *next;
struct LNK_ImportFunc *first_func;
struct LNK_ImportFunc *last_func;
LNK_Chunk *dll_chunk;
LNK_Chunk *int_table_chunk;
LNK_Chunk *ilt_table_chunk;
LNK_Chunk *iat_table_chunk;
LNK_Chunk *biat_table_chunk;
LNK_Chunk *uiat_table_chunk;
LNK_Chunk *code_table_chunk;
LNK_Symbol *tail_merge_symbol;
String8 name;
COFF_MachineType machine;
HashTable *func_ht;
} LNK_ImportDLL;
enum
{
LNK_ImportTableFlag_EmitBiat = (1 << 0),
LNK_ImportTableFlag_EmitUiat = (1 << 1),
};
typedef U32 LNK_ImportTableFlags;
typedef struct LNK_ImportTable
{
Arena *arena;
COFF_MachineType machine;
LNK_ImportDLL *first_dll;
LNK_ImportDLL *last_dll;
LNK_Section *data_sect;
LNK_Section *code_sect;
LNK_Chunk *dll_table_chunk;
LNK_Chunk *int_chunk;
LNK_Chunk *handle_table_chunk;
LNK_Chunk *iat_chunk;
LNK_Chunk *ilt_chunk;
LNK_Chunk *biat_chunk;
LNK_Chunk *uiat_chunk;
LNK_Chunk *code_chunk;
LNK_ImportTableFlags flags;
HashTable *dll_ht;
} LNK_ImportTable;
internal LNK_ImportTable * lnk_import_table_alloc_regular(LNK_SectionTable *st, LNK_SymbolTable *symtab, COFF_MachineType machine);
internal LNK_ImportTable * lnk_import_table_alloc_delayed(LNK_SectionTable *st, LNK_SymbolTable *symtab, COFF_MachineType machine, B32 is_unloadable, B32 is_bindable);
internal void lnk_import_table_release(LNK_ImportTable **imptab);
internal LNK_ImportDLL * lnk_import_table_push_dll_regular(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, String8 dll_name, COFF_MachineType machine);
internal LNK_ImportDLL * lnk_import_table_push_dll_delayed(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, String8 dll_name, COFF_MachineType machine);
internal LNK_ImportFunc * lnk_import_table_push_func_regular(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, LNK_ImportDLL *dll, COFF_ImportHeader *header);
internal LNK_ImportFunc * lnk_import_table_push_func_delayed(LNK_ImportTable *imptab, LNK_SymbolTable *symtab, LNK_ImportDLL *dll, COFF_ImportHeader *header);
internal LNK_ImportDLL * lnk_import_table_search_dll(LNK_ImportTable *imptab, String8 name);
internal LNK_ImportFunc * lnk_import_table_search_func(LNK_ImportDLL *dll, String8 name);
internal String8 lnk_ordinal_data_from_hint(Arena *arena, COFF_MachineType machine, U16 hint);
internal LNK_Chunk * lnk_emit_indirect_jump_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *addr_ptr);
internal LNK_Chunk * lnk_emit_load_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *imp_addr_ptr, LNK_Symbol *tail_merge);
internal LNK_Chunk * lnk_emit_tail_merge_thunk_x64(LNK_Section *sect, LNK_Chunk *parent, LNK_Symbol *dll_import_descriptor, LNK_Symbol *delay_load_helper);
internal LNK_Symbol * lnk_emit_load_thunk_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name);
internal LNK_Symbol * lnk_emit_jmp_thunk_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name);
internal LNK_Symbol * lnk_emit_tail_merge_symbol(LNK_SymbolTable *symtab, LNK_Chunk *chunk, String8 func_name);
+941
View File
@@ -0,0 +1,941 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal LNK_LibNode *
lnk_lib_list_reserve(Arena *arena, LNK_LibList *list, U64 count)
{
LNK_LibNode *arr = 0;
if (count) {
arr = push_array(arena, LNK_LibNode, count);
for (LNK_LibNode *ptr = arr, *opl = arr + count; ptr < opl; ++ptr) {
SLLQueuePush(list->first, list->last, ptr);
}
list->count += count;
}
return arr;
}
internal LNK_LibMemberNode *
lnk_lib_member_list_push(Arena *arena, LNK_LibMemberList *list, LNK_LibMember member)
{
LNK_LibMemberNode *n = push_array_no_zero(arena, LNK_LibMemberNode, 1);
n->next = 0;
n->data = member;
SLLQueuePush(list->first, list->last, n);
++list->count;
return n;
}
internal LNK_LibMember *
lnk_lib_member_array_from_list(Arena *arena, LNK_LibMemberList list)
{
ProfBeginFunction();
LNK_LibMember *arr = push_array_no_zero(arena, LNK_LibMember, list.count);
LNK_LibMember *ptr = arr;
for (LNK_LibMemberNode *i = list.first; i != 0; i = i->next, ptr += 1) {
ptr->name = push_str8_copy(arena, i->data.name);
ptr->data = push_str8_copy(arena, i->data.data);
}
ProfEnd();
return arr;
}
internal LNK_LibSymbolNode *
lnk_lib_symbol_list_push(Arena *arena, LNK_LibSymbolList *list, LNK_LibSymbol symbol)
{
LNK_LibSymbolNode *n = push_array_no_zero(arena, LNK_LibSymbolNode, 1);
n->next = 0;
n->data = symbol;
SLLQueuePush(list->first, list->last, n);
++list->count;
return n;
}
internal LNK_LibSymbol *
lnk_lib_symbol_array_from_list(Arena *arena, LNK_LibSymbolList list)
{
LNK_LibSymbol *arr = push_array_no_zero(arena, LNK_LibSymbol, list.count + 2);
LNK_LibSymbol *ptr = arr + 1;
for (LNK_LibSymbolNode *i = list.first; i != 0; i = i->next, ptr += 1) {
ptr->name = push_str8_copy(arena, i->data.name);
ptr->member_idx = i->data.member_idx;
}
MemoryZeroStruct(&arr[0]);
MemoryZeroStruct(&arr[list.count+1]);
return arr;
}
int
lnk_lib_symbol_name_compar(const void *raw_a, const void *raw_b)
{
const LNK_Symbol *sa = (const LNK_Symbol *)raw_a;
const LNK_Symbol *sb = (const LNK_Symbol *)raw_b;
return str8_compar_case_sensetive(&sa->name, &sb->name);
}
int
lnk_lib_symbol_name_compar_is_before(void *raw_a, void *raw_b)
{
int compar = lnk_lib_symbol_name_compar(raw_a, raw_b);
int is_before = compar < 0;
return is_before;
}
internal void
lnk_lib_symbol_array_sort(LNK_LibSymbol *arr, U64 count)
{
Assert(count >= 2);
radsort(arr + 1, count - 2, lnk_lib_symbol_name_compar_is_before);
}
////////////////////////////////
internal LNK_Lib
lnk_lib_from_data(Arena *arena, String8 data, String8 path)
{
U64 symbol_count;
String8 string_table;
U32 *member_off_arr;
// is data archive?
COFF_ArchiveType type = coff_archive_type_from_data(data);
if (type == COFF_Archive_Null) {
lnk_not_implemented("TODO: data is not archive");
}
COFF_ArchiveParse parse = coff_archive_parse_from_data(arena, data);
// try to init library from optional second member
if (parse.second_member.member_count) {
COFF_ArchiveSecondMember second_member = parse.second_member;
Assert(second_member.symbol_count == second_member.symbol_indices.size / sizeof(U16));
Assert(second_member.member_count == second_member.member_offsets.size / sizeof(U32));
symbol_count = second_member.symbol_count;
string_table = second_member.string_table;
member_off_arr = push_array_no_zero(arena, U32, symbol_count);
// decompress member offsets
U32 *comp_off_arr = (U32*)second_member.member_offsets.str;
U16 *off_number_arr = (U16*)second_member.symbol_indices.str;
for (U64 symbol_idx = 0; symbol_idx < symbol_count; symbol_idx += 1) {
U16 off_number = off_number_arr[symbol_idx];
if (0 < off_number && off_number <= second_member.member_count) {
member_off_arr[symbol_idx] = comp_off_arr[off_number - 1];
} else {
// TODO: log bad offset
member_off_arr[symbol_idx] = max_U32;
}
}
}
// first member is deprecated however tools emit it for compatibility reasons
// and lld-link with /DLL emits only first member
else if (parse.first_member.symbol_count) {
COFF_ArchiveFirstMember first_member = parse.first_member;
Assert(first_member.symbol_count == first_member.member_offsets.size / sizeof(U32));
symbol_count = first_member.symbol_count;
string_table = first_member.string_table;
member_off_arr = (U32*)first_member.member_offsets.str;
// convert big endian offsets
for (U32 offset_idx = 0; offset_idx < symbol_count; offset_idx += 1) {
member_off_arr[offset_idx] = BE_U32(member_off_arr[offset_idx]);
}
} else {
symbol_count = 0;
string_table = str8(0,0);
member_off_arr = 0;
}
// parse string table
String8List symbol_name_list = str8_split_by_string_chars(arena, string_table, str8_lit("\0"), StringSplitFlag_KeepEmpties);
Assert(symbol_name_list.node_count >= symbol_count);
symbol_count = Min(symbol_count, symbol_name_list.node_count);
// init lib
LNK_Lib lib = {0};
lib.path = push_str8_copy(arena, path);
lib.data = data;
lib.type = type;
lib.symbol_count = symbol_count;
lib.member_off_arr = member_off_arr;
lib.symbol_name_list = symbol_name_list;
lib.long_names = parse.long_names;
return lib;
}
THREAD_POOL_TASK_FUNC(lnk_lib_initer)
{
LNK_LibIniter *task = raw_task;
LNK_LibNode *lib_node = task->node_arr + task_id;
LNK_Lib *lib = &lib_node->data;
String8 data = task->data_arr[task_id];
String8 path = task->path_arr[task_id];
*lib = lnk_lib_from_data(arena, data, path);
}
internal LNK_LibNodeArray
lnk_lib_list_push_parallel(TP_Context *tp, TP_Arena *arena, LNK_LibList *list, String8Array data_arr, String8Array path_arr)
{
Assert(data_arr.count == path_arr.count);
U64 lib_count = data_arr.count;
LNK_LibIniter lib_initer = {0};
lib_initer.node_arr = lnk_lib_list_reserve(arena->v[0], list, lib_count);
lib_initer.data_arr = data_arr.v;
lib_initer.path_arr = path_arr.v;
tp_for_parallel(tp, arena, lib_count, lnk_lib_initer, &lib_initer);
LNK_LibNodeArray arr;
arr.count = lib_count;
arr.v = lib_initer.node_arr;
return arr;
}
#if 0
internal LNK_LibNode *
lnk_lib_list_push(Arena *arena, LNK_LibList *list, String8 data, String8 path)
{
ProfBeginFunction();
TP_Arena pool_arena = {0};
pool_arena.count = 1;
pool_arena.v = &arena;
String8Array data_arr = {0};
data_arr.count = 1;
data_arr.v = &data;
String8Array path_arr = {0};
path_arr.count = 1;
path_arr.v = &path;
LNK_LibNodeArray node_arr = lnk_lib_list_push_parallel(&pool_arena, list, data_arr, path_arr);
ProfEnd();
return node_arr.v;
}
#endif
////////////////////////////////
internal LNK_LibWriter *
lnk_lib_writer_alloc(void)
{
Arena *arena = arena_alloc();
LNK_LibWriter *writer = push_array(arena, LNK_LibWriter, 1);
writer->arena = arena;
return writer;
}
internal void
lnk_lib_writer_release(LNK_LibWriter **writer_ptr)
{
arena_release((*writer_ptr)->arena);
*writer_ptr = 0;
}
internal void
lnk_lib_writer_push_obj(LNK_LibWriter *writer, LNK_Obj *obj)
{
ProfBeginFunction();
U64 member_idx = writer->member_list.count;
// push obj member
LNK_LibMember member = {0};
member.name = obj->path;
member.data = obj->data;
lnk_lib_member_list_push(writer->arena, &writer->member_list, member);
// push external symbols
for (LNK_SymbolNode *node = obj->symbol_list.first; node != 0; node = node->next) {
LNK_Symbol *symbol = node->data;
B32 is_extern = symbol->type == LNK_Symbol_DefinedExtern;
if (is_extern) {
LNK_LibSymbol lib_symbol = {0};
lib_symbol.name = symbol->name;
lib_symbol.member_idx = member_idx;
lnk_lib_symbol_list_push(writer->arena, &writer->symbol_list, lib_symbol);
}
}
ProfEnd();
}
internal void
lnk_lib_writer_push_export(LNK_LibWriter *writer, COFF_MachineType machine, U64 time_stamp, String8 dll_name, LNK_Export *exp)
{
ProfBeginFunction();
U64 member_idx = writer->member_list.count;
// make import header
String8 import_data;
if (exp->name.size) {
U16 hint = safe_cast_u16(exp->id);
import_data = coff_make_import_header_by_name(writer->arena, dll_name, machine, time_stamp, exp->name, hint, exp->type);
} else {
U16 ordinal = safe_cast_u16(exp->id);
import_data = coff_make_import_header_by_ordinal(writer->arena, dll_name, machine, time_stamp, ordinal, exp->type);
}
// push import member
LNK_LibMember member = {0};
member.name = dll_name;
member.data = import_data;
lnk_lib_member_list_push(writer->arena, &writer->member_list, member);
switch (exp->type) {
case COFF_ImportHeaderType_CODE: {
LNK_LibSymbol def_symbol = {0};
def_symbol.name = push_str8_copy(writer->arena, exp->name);
def_symbol.member_idx = member_idx;
lnk_lib_symbol_list_push(writer->arena, &writer->symbol_list, def_symbol);
}
case COFF_ImportHeaderType_DATA: {
LNK_LibSymbol imp_symbol = {0};
imp_symbol.name = push_str8f(writer->arena, "__imp_%S", exp->name);
imp_symbol.member_idx = member_idx;
lnk_lib_symbol_list_push(writer->arena, &writer->symbol_list, imp_symbol);
} break;
case COFF_ImportHeaderType_CONST: {
NotImplemented;
} break;
default: InvalidPath;
}
ProfEnd();
}
internal LNK_LibBuild
lnk_lib_build_from_writer(Arena *arena, LNK_LibWriter *writer)
{
ProfBeginFunction();
LNK_LibBuild lib = {0};
lib.symbol_count = writer->symbol_list.count + 2;
lib.member_count = writer->member_list.count;
lib.symbol_array = lnk_lib_symbol_array_from_list(arena, writer->symbol_list);
lib.member_array = lnk_lib_member_array_from_list(arena, writer->member_list);
lnk_lib_symbol_array_sort(lib.symbol_array, lib.symbol_count);
ProfEnd();
return lib;
}
internal String8List
lnk_coff_archive_from_lib_build(Arena *arena, LNK_LibBuild *lib, B32 emit_second_member, COFF_TimeStamp time_stamp, U32 mode)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
U64 symbol_count = lib->symbol_count - 2;
LNK_LibSymbol *symbol_arr = lib->symbol_array + 1;
HashTable *name_ht = hash_table_init(scratch.arena, 1024);
U64 *member_off_arr = push_array_no_zero(scratch.arena, U64, lib->member_count);
String8List long_names_list = {0};
String8List member_data_list = {0};
for (U64 member_idx = 0; member_idx < lib->member_count; member_idx += 1) {
LNK_LibMember *member = &lib->member_array[member_idx];
// make member name
String8 name;
U64 name_with_slash_size = member->name.size + 1;
if (name_with_slash_size > COFF_ARCHIVE_MAX_SHORT_NAME_SIZE) {
// have we seen this member name before?
KeyValuePair *is_present = hash_table_search_string(name_ht, member->name);
if (is_present) {
name = is_present->value_string;
} else {
name = push_str8f(scratch.arena, "/%u", long_names_list.total_size);
str8_list_pushf(scratch.arena, &long_names_list, "%S/\n", member->name);
hash_table_push_string_string(scratch.arena, name_ht, member->name, name);
}
} else {
name = push_str8f(scratch.arena, "%S/", member->name);
}
member_off_arr[member_idx] = member_data_list.total_size;
String8 member_data = member->data;
String8 member_header = lnk_build_lib_member_header(arena, name, time_stamp, 0, 0, mode, member_data.size);
str8_list_push(arena, &member_data_list, member_header);
str8_list_push(arena, &member_data_list, member_data);
str8_list_push_pad(arena, &member_data_list, member_data_list.total_size, COFF_ARCHIVE_ALIGN);
}
// long names member
if (long_names_list.total_size > 0) {
String8 header = lnk_build_lib_member_header(arena, str8_lit("//"), time_stamp, 0, 0, mode, long_names_list.total_size);
String8 data = str8_list_join(arena, &long_names_list, 0);
U64 member_offset = member_data_list.total_size + data.size + header.size;
str8_list_push_pad_front(arena, &member_data_list, member_offset, COFF_ARCHIVE_ALIGN);
str8_list_push_front(arena, &member_data_list, data);
str8_list_push_front(arena, &member_data_list, header);
}
// compute size for symbol string table
U32 name_buffer_size = 0;
for (LNK_LibSymbol *ptr = &symbol_arr[0], *opl = ptr + symbol_count; ptr < opl; ptr += 1) {
name_buffer_size += ptr->name.size;
name_buffer_size += 1; // null
}
// write symbol name buffer
U8 *name_buffer = push_array_no_zero(scratch.arena, U8, name_buffer_size);
{
U64 name_cursor = 0;
for (LNK_LibSymbol *ptr = &symbol_arr[0], *opl = ptr + symbol_count; ptr < opl; ptr += 1) {
MemoryCopy(name_buffer + name_cursor, ptr->name.str, ptr->name.size);
name_buffer[name_cursor + ptr->name.size] = '\0';
name_cursor += ptr->name.size + 1;
}
}
U64 member_base_off;
{
U64 sizeof_first_header = COFF_ARCHIVE_MEMBER_HEADER_SIZE + sizeof(U32) + sizeof(U32) * symbol_count + name_buffer_size;
U64 sizeof_second_header = COFF_ARCHIVE_MEMBER_HEADER_SIZE + sizeof(U32) + sizeof(U32) * lib->member_count + sizeof(U32) + sizeof(U16) * symbol_count + name_buffer_size;
U64 sizeof_long_names = COFF_ARCHIVE_MEMBER_HEADER_SIZE + long_names_list.total_size;
sizeof_first_header = AlignPow2(sizeof_first_header, COFF_ARCHIVE_ALIGN);
sizeof_second_header = AlignPow2(sizeof_second_header, COFF_ARCHIVE_ALIGN);
sizeof_long_names = AlignPow2(sizeof_long_names, COFF_ARCHIVE_ALIGN);
member_base_off = sizeof(g_coff_archive_sig);
member_base_off += sizeof_first_header;
if (emit_second_member) {
member_base_off += sizeof_second_header;
}
if (long_names_list.total_size > 0) {
member_base_off += sizeof_long_names;
}
}
// second linker member
if (emit_second_member) {
U32 member_count32 = safe_cast_u32(lib->member_count);
U32 symbol_count32 = safe_cast_u32(symbol_count);
U32 *member_off32_arr = push_array_no_zero(scratch.arena, U32, lib->member_count);
U16 *member_idx16_arr = push_array_no_zero(scratch.arena, U16, symbol_count);
// write member offset array
for (U64 member_idx = 0; member_idx < lib->member_count; member_idx += 1) {
U64 member_off = member_base_off + member_off_arr[member_idx];
U32 member_off32 = safe_cast_u32(member_off);
member_off32_arr[member_idx] = member_off32;
}
// write member offset indices for each symbol
for (U64 symbol_idx = 0; symbol_idx < symbol_count; symbol_idx += 1) {
// member offset indices are 1-based
U64 member_idx = symbol_arr[symbol_idx].member_idx + 1;
U16 member_idx16 = safe_cast_u16(member_idx);
member_idx16_arr[symbol_idx] = member_idx16;
}
// layout second member data
String8List second_member_data_list = {0};
str8_list_push(scratch.arena, &second_member_data_list, str8_struct(&member_count32));
str8_list_push(scratch.arena, &second_member_data_list, str8_array(member_off32_arr, lib->member_count));
str8_list_push(scratch.arena, &second_member_data_list, str8_struct(&symbol_count32));
str8_list_push(scratch.arena, &second_member_data_list, str8_array(member_idx16_arr, symbol_count));
str8_list_push(scratch.arena, &second_member_data_list, str8(name_buffer, name_buffer_size));
String8 member_data = str8_list_join(arena, &second_member_data_list, 0);
String8 member_header = lnk_build_lib_member_header(arena, str8_lit("/"), time_stamp, 0, 0, mode, member_data.size);
U64 member_offset = member_data_list.total_size + member_data.size + member_header.size;
str8_list_push_pad_front(arena, &member_data_list, member_offset, COFF_ARCHIVE_ALIGN);
str8_list_push_front(arena, &member_data_list, member_data);
str8_list_push_front(arena, &member_data_list, member_header);
}
// first linker member (obsolete, but kept for compatability reasons)
{
U32 symbol_count_be = BE_U32(symbol_count);
U32 *member_off32_arr = push_array_no_zero(scratch.arena, U32, symbol_count);
for (U64 symbol_idx = 0; symbol_idx < symbol_count; symbol_idx += 1) {
LNK_LibSymbol *symbol = &symbol_arr[symbol_idx];
// write big endian member offset
U64 member_off = member_base_off + member_off_arr[symbol->member_idx];
U32 member_off32 = BE_U32(safe_cast_u32(member_off));
member_off32_arr[symbol_idx] = member_off32;
}
// layout first member data
String8List first_member_data_list = {0};
str8_list_push(scratch.arena, &first_member_data_list, str8_struct(&symbol_count_be));
str8_list_push(scratch.arena, &first_member_data_list, str8_array(member_off32_arr, symbol_count));
str8_list_push(scratch.arena, &first_member_data_list, str8(name_buffer, name_buffer_size));
String8 member_data = str8_list_join(arena, &first_member_data_list, 0);
String8 member_header = lnk_build_lib_member_header(arena, str8_lit("/"), time_stamp, 0, 0, mode, member_data.size);
U64 member_offset = sizeof(g_coff_archive_sig) + member_header.size + member_data.size;
str8_list_push_pad_front(arena, &member_data_list, member_offset, COFF_ARCHIVE_ALIGN);
str8_list_push_front(arena, &member_data_list, member_data);
str8_list_push_front(arena, &member_data_list, member_header);
}
// archive signature
str8_list_push_front(arena, &member_data_list, str8_struct(&g_coff_archive_sig));
scratch_end(scratch);
ProfEnd();
return member_data_list;
}
////////////////////////////////
internal LNK_LibBuild
lnk_build_lib(Arena *arena, COFF_MachineType machine, COFF_TimeStamp time_stamp, String8 dll_name, LNK_ObjList obj_list, LNK_ExportTable *exptab)
{
ProfBeginFunction();
LNK_LibWriter *writer = lnk_lib_writer_alloc();
for (LNK_ObjNode *obj_node = obj_list.first; obj_node != 0; obj_node = obj_node->next) {
lnk_lib_writer_push_obj(writer, &obj_node->data);
}
for (LNK_Export *exp = exptab->name_export_list.first; exp != 0; exp = exp->next) {
lnk_lib_writer_push_export(writer, machine, time_stamp, dll_name, exp);
}
LNK_LibBuild lib = lnk_lib_build_from_writer(arena, writer);
lnk_lib_writer_release(&writer);
ProfEnd();
return lib;
}
internal String8List
lnk_build_import_entry_obj(Arena *arena, String8 dll_name, COFF_MachineType machine)
{
ProfBeginFunction();
Assert(machine == COFF_MachineType_X64);
Assert(str8_match(str8_lit("dll"), str8_skip_last_dot(dll_name), StringMatchFlag_CaseInsensitive|StringMatchFlag_RightSideSloppy));
String8List list = {0};
COFF_Header *coff_header = push_array(arena, COFF_Header, 1);
coff_header->machine = machine;
str8_list_push(arena, &list, str8_struct(coff_header));
coff_header->section_count = 2;
COFF_SectionHeader *coff_sect_header_array = push_array(arena, COFF_SectionHeader, coff_header->section_count);
str8_list_push(arena, &list, str8_array(coff_sect_header_array, coff_header->section_count));
PE_ImportEntry *import_entry = push_array(arena, PE_ImportEntry, 1);
U64 import_entry_off = list.total_size;
str8_list_push(arena, &list, str8_struct(import_entry));
String8 dll_name_cstr = push_cstr(arena, dll_name);
U64 dll_name_off = list.total_size;
str8_list_push(arena, &list, dll_name_cstr);
U32 import_entry_reloc_count = 3;
COFF_Reloc *import_entry_reloc_array = push_array(arena, COFF_Reloc, import_entry_reloc_count);
U64 import_entry_reloc_off = list.total_size;
str8_list_push(arena, &list, str8_array(import_entry_reloc_array, import_entry_reloc_count));
coff_header->symbol_count = 7;
COFF_Symbol16 *symbol_array = push_array(arena, COFF_Symbol16, coff_header->symbol_count);
coff_header->symbol_table_foff = safe_cast_u32(list.total_size);
str8_list_push(arena, &list, str8_array(symbol_array, coff_header->symbol_count));
U64 string_table_base = list.total_size;
U32 *string_table_size_ptr = push_array(arena, U32, 1);
str8_list_push(arena, &list, str8_struct(string_table_size_ptr));
// PE_ImportEntry
{
COFF_SectionHeader *sect = &coff_sect_header_array[0];
sect->name[0] = '.';
sect->name[1] = 'i';
sect->name[2] = 'd';
sect->name[3] = 'a';
sect->name[4] = 't';
sect->name[5] = 'a';
sect->name[6] = '$';
sect->name[7] = '2';
sect->fsize = sizeof(PE_ImportEntry);
sect->foff = import_entry_off;
sect->reloc_count = import_entry_reloc_count;
sect->relocs_foff = import_entry_reloc_off;
sect->flags = COFF_SectionFlag_CNT_INITIALIZED_DATA|(COFF_SectionAlign_4BYTES << COFF_SectionFlag_ALIGN_SHIFT)|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE;
}
{
COFF_Reloc *lookup_table_voff_reloc = &import_entry_reloc_array[0];
lookup_table_voff_reloc->apply_off = OffsetOf(PE_ImportEntry, lookup_table_voff);
lookup_table_voff_reloc->isymbol = 3;
lookup_table_voff_reloc->type = COFF_RelocTypeX64_ADDR32NB;
COFF_Reloc *name_voff_reloc = &import_entry_reloc_array[1];
name_voff_reloc->apply_off = OffsetOf(PE_ImportEntry, name_voff);
name_voff_reloc->isymbol = 2;
name_voff_reloc->type = COFF_RelocTypeX64_ADDR32NB;
COFF_Reloc *import_addr_table_voff = &import_entry_reloc_array[2];
import_addr_table_voff->apply_off = OffsetOf(PE_ImportEntry, import_addr_table_voff);
import_addr_table_voff->isymbol = 4;
import_addr_table_voff->type = COFF_RelocTypeX64_ADDR32NB;
}
// dll name
{
COFF_SectionHeader *sect = &coff_sect_header_array[1];
sect->name[0] = '.';
sect->name[1] = 'i';
sect->name[2] = 'd';
sect->name[3] = 'a';
sect->name[4] = 't';
sect->name[5] = 'a';
sect->name[6] = '$';
sect->name[7] = '6';
sect->fsize = dll_name_cstr.size;
sect->foff = dll_name_off;
sect->flags = COFF_SectionFlag_CNT_INITIALIZED_DATA|(COFF_SectionAlign_2BYTES << COFF_SectionFlag_ALIGN_SHIFT)|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE;
}
// import descriptor
{
String8 dll_name_no_ext = str8_substr(dll_name, r1u64(0, dll_name.size - 4));
String8 symbol_name = push_str8f(arena, "__IMPORT_DESCRIPTOR_%S", dll_name_no_ext);
U64 symbol_name_off = (list.total_size - string_table_base);
str8_list_push(arena, &list, push_cstr(arena, symbol_name));
COFF_Symbol16 *symbol = &symbol_array[0];
symbol->name.long_name.zeroes = 0;
symbol->name.long_name.string_table_offset = symbol_name_off;
symbol->section_number = 1;
symbol->storage_class = COFF_SymStorageClass_EXTERNAL;
}
// .idata$2
{
COFF_Symbol16 *symbol = &symbol_array[1];
symbol->name.short_name[0] = '.';
symbol->name.short_name[1] = 'i';
symbol->name.short_name[2] = 'd';
symbol->name.short_name[3] = 'a';
symbol->name.short_name[4] = 't';
symbol->name.short_name[5] = 'a';
symbol->name.short_name[6] = '$';
symbol->name.short_name[7] = '2';
symbol->section_number = 1;
symbol->storage_class = COFF_SymStorageClass_SECTION;
}
// .idata$6
{
COFF_Symbol16 *symbol = &symbol_array[2];
symbol->name.short_name[0] = '.';
symbol->name.short_name[1] = 'i';
symbol->name.short_name[2] = 'd';
symbol->name.short_name[3] = 'a';
symbol->name.short_name[4] = 't';
symbol->name.short_name[5] = 'a';
symbol->name.short_name[6] = '$';
symbol->name.short_name[7] = '6';
symbol->section_number = 2;
symbol->storage_class = COFF_SymStorageClass_STATIC;
}
// .idata$4
{
COFF_Symbol16 *symbol = &symbol_array[3];
symbol->name.short_name[0] = '.';
symbol->name.short_name[1] = 'i';
symbol->name.short_name[2] = 'd';
symbol->name.short_name[3] = 'a';
symbol->name.short_name[4] = 't';
symbol->name.short_name[5] = 'a';
symbol->name.short_name[6] = '$';
symbol->name.short_name[7] = '4';
symbol->section_number = COFF_SYMBOL_UNDEFINED_SECTION;
symbol->storage_class = COFF_SymStorageClass_SECTION;
}
// .idata$5
{
COFF_Symbol16 *symbol = &symbol_array[4];
symbol->name.short_name[0] = '.';
symbol->name.short_name[1] = 'i';
symbol->name.short_name[2] = 'd';
symbol->name.short_name[3] = 'a';
symbol->name.short_name[4] = 't';
symbol->name.short_name[5] = 'a';
symbol->name.short_name[6] = '$';
symbol->name.short_name[7] = '5';
symbol->section_number = COFF_SYMBOL_UNDEFINED_SECTION;
symbol->storage_class = COFF_SymStorageClass_SECTION;
}
// __NULL_IMPORT_DESCRIPTOR
{
U64 symbol_name_off = (list.total_size - string_table_base);
str8_list_push(arena, &list, push_cstr(arena, str8_lit("__NULL_IMPORT_DESCRIPTOR")));
COFF_Symbol16 *symbol = &symbol_array[5];
symbol->name.long_name.zeroes = 0;
symbol->name.long_name.string_table_offset = symbol_name_off;
symbol->section_number = COFF_SYMBOL_UNDEFINED_SECTION;
symbol->storage_class = COFF_SymStorageClass_EXTERNAL;
}
// NULL_THUNK_DATA
{
String8 dll_name_no_ext = str8_substr(dll_name, r1u64(0, dll_name.size - 4));
String8 symbol_name = push_str8f(arena, "\x7f%S_NULL_THUNK_DATA", dll_name_no_ext);
U64 symbol_name_off = (list.total_size - string_table_base);
str8_list_push(arena, &list, push_cstr(arena, symbol_name));
COFF_Symbol16 *symbol = &symbol_array[6];
symbol->name.long_name.zeroes = 0;
symbol->name.long_name.string_table_offset = symbol_name_off;
symbol->section_number = COFF_SYMBOL_UNDEFINED_SECTION;
symbol->storage_class = COFF_SymStorageClass_EXTERNAL;
}
// update string table size
*string_table_size_ptr = (list.total_size - string_table_base);
ProfEnd();
return list;
}
internal String8List
lnk_build_null_import_descriptor_obj(Arena *arena, COFF_MachineType machine)
{
ProfBeginFunction();
String8List list = {0};
COFF_Header *coff_header = push_array(arena, COFF_Header, 1);
coff_header->machine = machine;
str8_list_push(arena, &list, str8_struct(coff_header));
coff_header->section_count = 1;
COFF_SectionHeader *coff_sect_header_array = push_array(arena, COFF_SectionHeader, coff_header->section_count);
str8_list_push(arena, &list, str8_array(coff_sect_header_array, coff_header->section_count));
U64 null_import_data_size = 20;
U8 *null_import_data = push_array(arena, U8, null_import_data_size);
U64 null_import_data_off = list.total_size;
str8_list_push(arena, &list, str8(null_import_data, null_import_data_size));
coff_header->symbol_count = 1;
COFF_Symbol16 *symbol_array = push_array(arena, COFF_Symbol16, coff_header->symbol_count);
coff_header->symbol_table_foff = safe_cast_u32(list.total_size);
str8_list_push(arena, &list, str8_array(symbol_array, coff_header->symbol_count));
U64 string_table_base = list.total_size;
U32 *string_table_size_ptr = push_array(arena, U32, 1);
str8_list_push(arena, &list, str8_struct(string_table_size_ptr));
{
COFF_SectionHeader *sect = &coff_sect_header_array[0];
sect->name[0] = '.';
sect->name[1] = 'i';
sect->name[2] = 'd';
sect->name[3] = 'a';
sect->name[4] = 't';
sect->name[5] = 'a';
sect->name[6] = '$';
sect->name[7] = '3';
sect->fsize = null_import_data_size;
sect->foff = null_import_data_off;
sect->flags = COFF_SectionFlag_CNT_INITIALIZED_DATA|(COFF_SectionAlign_4BYTES << COFF_SectionFlag_ALIGN_SHIFT)|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE;
}
{
U64 symbol_name_off = list.total_size - string_table_base;
str8_list_push(arena, &list, push_cstr(arena, str8_lit("__NULL_IMPORT_DESCRIPTOR")));
COFF_Symbol16 *symbol = &symbol_array[0];
symbol->name.long_name.zeroes = 0;
symbol->name.long_name.string_table_offset = symbol_name_off;
symbol->section_number = 1;
symbol->storage_class = COFF_SymStorageClass_EXTERNAL;
}
// update string table size
*string_table_size_ptr = (list.total_size - string_table_base);
ProfEnd();
return list;
}
internal String8List
lnk_build_null_thunk_data_obj(Arena *arena, String8 dll_name, COFF_MachineType machine)
{
ProfBeginFunction();
Assert(str8_match(str8_lit("dll"), str8_skip_last_dot(dll_name), StringMatchFlag_CaseInsensitive|StringMatchFlag_RightSideSloppy));
String8List list = {0};
COFF_Header *coff_header = push_array(arena, COFF_Header, 1);
coff_header->machine = machine;
str8_list_push(arena, &list, str8_struct(coff_header));
coff_header->section_count = 2;
COFF_SectionHeader *coff_sect_header_array = push_array(arena, COFF_SectionHeader, coff_header->section_count);
str8_list_push(arena, &list, str8_array(coff_sect_header_array, coff_header->section_count));
U64 lookup_entry_data_size = 8;
U8 *lookup_entry_data = push_array(arena, U8, lookup_entry_data_size);
U64 lookup_entry_data_off = list.total_size;
str8_list_push(arena, &list, str8(lookup_entry_data, lookup_entry_data_size));
U64 null_thunk_data_size = 8;
U8 *null_thunk_data = push_array(arena, U8, null_thunk_data_size);
U64 null_thunk_data_off = list.total_size;
str8_list_push(arena, &list, str8(null_thunk_data, null_thunk_data_size));
coff_header->symbol_count = 1;
COFF_Symbol16 *symbol_array = push_array(arena, COFF_Symbol16, coff_header->symbol_count);
coff_header->symbol_table_foff = safe_cast_u32(list.total_size);
str8_list_push(arena, &list, str8_array(symbol_array, coff_header->symbol_count));
U64 string_table_base = list.total_size;
U32 *string_table_size_ptr = push_array(arena, U32, 1);
str8_list_push(arena, &list, str8_struct(string_table_size_ptr));
{
COFF_SectionHeader *sect = &coff_sect_header_array[0];
sect->name[0] = '.';
sect->name[1] = 'i';
sect->name[2] = 'd';
sect->name[3] = 'a';
sect->name[4] = 't';
sect->name[5] = 'a';
sect->name[6] = '$';
sect->name[7] = '5';
sect->fsize = lookup_entry_data_size;
sect->foff = lookup_entry_data_off;
sect->flags = COFF_SectionFlag_CNT_INITIALIZED_DATA|(COFF_SectionAlign_8BYTES << COFF_SectionFlag_ALIGN_SHIFT)|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE;
}
{
COFF_SectionHeader *sect = &coff_sect_header_array[1];
sect->name[0] = '.';
sect->name[1] = 'i';
sect->name[2] = 'd';
sect->name[3] = 'a';
sect->name[4] = 't';
sect->name[5] = 'a';
sect->name[6] = '$';
sect->name[7] = '4';
sect->fsize = null_thunk_data_size;
sect->foff = null_thunk_data_off;
sect->flags = COFF_SectionFlag_CNT_INITIALIZED_DATA|(COFF_SectionAlign_8BYTES << COFF_SectionFlag_ALIGN_SHIFT)|COFF_SectionFlag_MEM_READ|COFF_SectionFlag_MEM_WRITE;
}
{
String8 dll_name_no_ext = str8_substr(dll_name, r1u64(0, dll_name.size - 4));
String8 symbol_name = push_str8f(arena, "\x7f%S_NULL_THUNK_DATA", dll_name_no_ext);
U64 symbol_name_off = list.total_size - string_table_base;
str8_list_push(arena, &list, push_cstr(arena, symbol_name));
COFF_Symbol16 *symbol = &symbol_array[0];
symbol->name.long_name.zeroes = 0;
symbol->name.long_name.string_table_offset = symbol_name_off;
symbol->section_number = 1;
symbol->storage_class = COFF_SymStorageClass_EXTERNAL;
}
// update string table size
*string_table_size_ptr = (list.total_size - string_table_base);
ProfEnd();
return list;
}
internal String8
lnk_build_lib_member_header(Arena *arena, String8 name, COFF_TimeStamp time_stamp, U16 user_id, U16 group_id, U16 mode, U32 size)
{
ProfBeginFunction();
Assert(name.size < 16);
Assert(user_id < 10000);
Assert(group_id < 10000);
Assert(mode < 10000);
Assert(size < 1000000000);
//U64 sizeof_member_header = /* name */ 16 + /* time */ 12 + /* user_id */ 6 + /* group id */ 6 + /* mode */ 8 + /* size */ 10;
Temp scratch = scratch_begin(&arena, 1);
String8List list = {0};
str8_list_pushf(scratch.arena, &list, "%-16.*s", str8_varg(name));
str8_list_pushf(scratch.arena, &list, "%-12u", time_stamp);
str8_list_pushf(scratch.arena, &list, "%-6u", user_id);
str8_list_pushf(scratch.arena, &list, "%-6u", group_id);
str8_list_pushf(scratch.arena, &list, "%-8u", mode);
str8_list_pushf(scratch.arena, &list, "%-10u", size);
str8_list_pushf(scratch.arena, &list, "`\n");
String8 result = str8_list_join(arena, &list, 0);
Assert(result.size == COFF_ARCHIVE_MEMBER_HEADER_SIZE);
scratch_end(scratch);
ProfEnd();
return result;
}
internal String8List
lnk_build_import_lib(TP_Context *tp, TP_Arena *arena, COFF_MachineType machine, COFF_TimeStamp time_stamp, String8 lib_name, String8 dll_name, LNK_ExportTable *exptab)
{
ProfBeginFunction();
Temp scratch = scratch_begin(arena->v, arena->count);
dll_name = str8_skip_last_slash(dll_name);
// These objects appear in first three members of any lib that linker produces with /dll.
// Objects are used by MSVC linker to build import table.
String8List import_obj_array[3];
import_obj_array[0] = lnk_build_import_entry_obj(scratch.arena, dll_name, machine);
import_obj_array[1] = lnk_build_null_import_descriptor_obj(scratch.arena, machine);
import_obj_array[2] = lnk_build_null_thunk_data_obj(scratch.arena, dll_name, machine);
// build input list
LNK_InputObjList input_obj_list = {0};
for (U64 i = 0; i < ArrayCount(import_obj_array); ++i) {
LNK_InputObj *input = lnk_input_obj_list_push(scratch.arena, &input_obj_list);
input->data = str8_list_join(scratch.arena, &import_obj_array[i], 0);
input->path = dll_name;
input->lib_path = lib_name;
}
LNK_InputObj **inputs = lnk_array_from_input_obj_list(scratch.arena, input_obj_list);
LNK_SectionTable *st = lnk_section_table_alloc(0,0,0);
LNK_ObjList obj_list = {0};
lnk_obj_list_push_parallel(tp, arena, &obj_list, st, input_obj_list.count, inputs);
LNK_LibBuild import_lib = lnk_build_lib(scratch.arena, machine, time_stamp, dll_name, obj_list, exptab);
B32 emit_second_member = 0; // MSVC linker refuses to link with lib that has the second member.
String8List coff_archive_data = lnk_coff_archive_from_lib_build(arena->v[0], &import_lib, emit_second_member, time_stamp, /* -rw-r--r-- */ 644);
// cleanup memory
lnk_section_table_release(&st);
scratch_end(scratch);
ProfEnd();
return coff_archive_data;
}
+134
View File
@@ -0,0 +1,134 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct LNK_Lib
{
String8 path;
String8 data;
COFF_ArchiveType type;
U32 symbol_count;
U32 * member_off_arr;
String8List symbol_name_list;
String8 long_names;
} LNK_Lib;
typedef struct LNK_LibNode
{
struct LNK_LibNode *next;
LNK_Lib data;
} LNK_LibNode;
typedef struct LNK_LibNodeArray
{
U64 count;
LNK_LibNode *v;
} LNK_LibNodeArray;
typedef struct LNK_LibList
{
U64 count;
struct LNK_LibNode *first;
struct LNK_LibNode *last;
} LNK_LibList;
////////////////////////////////
typedef struct LNK_LibMember
{
String8 name;
String8 data;
} LNK_LibMember;
typedef struct LNK_LibMemberNode
{
struct LNK_LibMemberNode *next;
LNK_LibMember data;
} LNK_LibMemberNode;
typedef struct LNK_LibMemberList
{
U64 count;
LNK_LibMemberNode *first;
LNK_LibMemberNode *last;
} LNK_LibMemberList;
typedef struct LNK_LibSymbol
{
String8 name;
U64 member_idx;
} LNK_LibSymbol;
typedef struct LNK_LibSymbolNode
{
struct LNK_LibSymbolNode *next;
LNK_LibSymbol data;
} LNK_LibSymbolNode;
typedef struct LNK_LibSymbolList
{
U64 count;
LNK_LibSymbolNode *first;
LNK_LibSymbolNode *last;
} LNK_LibSymbolList;
typedef struct LNK_LibWriter
{
Arena *arena;
LNK_LibMemberList member_list;
LNK_LibSymbolList symbol_list;
} LNK_LibWriter;
typedef struct LNK_LibBuild
{
U64 symbol_count;
U64 member_count;
LNK_LibSymbol *symbol_array;
LNK_LibMember *member_array;
} LNK_LibBuild;
////////////////////////////////
typedef struct
{
LNK_LibNode *node_arr;
String8 *data_arr;
String8 *path_arr;
} LNK_LibIniter;
////////////////////////////////
internal LNK_LibNode * lnk_lib_list_reserve(Arena *arena, LNK_LibList *list, U64 count);
internal LNK_LibMemberNode * lnk_lib_member_list_push(Arena *arena, LNK_LibMemberList *list, LNK_LibMember member);
internal LNK_LibMember * lnk_lib_member_array_from_list(Arena *arena, LNK_LibMemberList list);
internal LNK_LibSymbolNode * lnk_lib_symbol_list_push(Arena *arena, LNK_LibSymbolList *list, LNK_LibSymbol symbol);
internal LNK_LibSymbol * lnk_lib_symbol_array_from_list(Arena *arena, LNK_LibSymbolList list);
internal void lnk_lib_symbol_array_sort(LNK_LibSymbol *arr, U64 count);
////////////////////////////////
internal LNK_Lib lnk_lib_from_data(Arena *arena, String8 data, String8 path);
internal LNK_LibNodeArray lnk_lib_list_push_parallel(TP_Context *tp, TP_Arena *arena, LNK_LibList *list, String8Array data_arr, String8Array path_arr);
internal LNK_LibNode * lnk_lib_list_push(Arena *arena, LNK_LibList *list, String8 data, String8 path);
////////////////////////////////
internal LNK_LibWriter * lnk_lib_writer_alloc(void);
internal void lnk_lib_writer_release(LNK_LibWriter **writer_ptr);
internal void lnk_lib_writer_push_obj(LNK_LibWriter *writer, LNK_Obj *obj);
internal void lnk_lib_writer_push_export(LNK_LibWriter *writer, COFF_MachineType machine, U64 time_stamp, String8 dll_name, LNK_Export *exp);
internal LNK_LibBuild lnk_lib_build_from_writer(Arena *arena, LNK_LibWriter *writer);
internal String8List lnk_coff_archive_from_lib_build(Arena *arena, LNK_LibBuild *lib, B32 emit_second_member, COFF_TimeStamp time_stamp, U32 mode);
////////////////////////////////
internal LNK_LibBuild lnk_build_lib(Arena *arena, COFF_MachineType machine, COFF_TimeStamp time_stamp, String8 dll_name, LNK_ObjList obj_list, LNK_ExportTable *exptab);
internal String8List lnk_build_import_entry_obj(Arena *arena, String8 dll_name, COFF_MachineType machine);
internal String8List lnk_build_null_import_descriptor_obj(Arena *arena, COFF_MachineType machine);
internal String8List lnk_build_null_thunk_data_obj(Arena *arena, String8 dll_name, COFF_MachineType machine);
internal String8 lnk_build_lib_member_header(Arena *arena, String8 name, COFF_TimeStamp time_stamp, U16 user_id, U16 group_id, U16 mode, U32 size);
internal String8List lnk_build_import_lib(TP_Context *tp, TP_Arena *arena, COFF_MachineType machine, COFF_TimeStamp time_stamp, String8 lib_name, String8 dll_name, LNK_ExportTable *exptab);
+59
View File
@@ -0,0 +1,59 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal void
lnk_set_log_status(LNK_LogType type, B32 is_enabled)
{
g_log_status[type] = is_enabled;
}
internal B32
lnk_get_log_status(LNK_LogType type)
{
B32 status = g_log_status[type];
return status;
}
internal void
lnk_log(LNK_LogType type, char *fmt, ...)
{
B32 is_log_enabled = g_log_status[type];
if (is_log_enabled) {
Temp scratch = scratch_begin(0,0);
va_list args;
va_start(args, fmt);
String8 string = push_str8fv(scratch.arena, fmt, args);
fprintf(stdout, "%.*s\n", str8_varg(string));
va_end(args);
scratch_end(scratch);
}
}
internal LNK_LogType
lnk_log_type_from_string(String8 string)
{
static struct {
char *name;
LNK_LogType type;
} map[] = {
"Null", LNK_Log_Null,
"Debug", LNK_Log_Debug,
"InputObj", LNK_Log_InputObj,
"InputLib", LNK_Log_InputLib,
"IO", LNK_Log_IO,
"SizeBreakdown", LNK_Log_SizeBreakdown,
"LinkStats", LNK_Log_LinkStats,
"Timers", LNK_Log_Timers,
};
Assert(ArrayCount(map) == LNK_Log_Count);
for (U64 i = 0; i < ArrayCount(map); ++i) {
if (str8_match(str8_cstring(map[i].name), string, StringMatchFlag_CaseInsensitive)) {
return map[i].type;
}
}
return LNK_Log_Null;
}
+24
View File
@@ -0,0 +1,24 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum
{
LNK_Log_Null,
LNK_Log_Debug,
LNK_Log_InputObj,
LNK_Log_InputLib,
LNK_Log_IO,
LNK_Log_SizeBreakdown,
LNK_Log_LinkStats,
LNK_Log_Timers,
LNK_Log_Count
} LNK_LogType;
internal void set_log_level(LNK_LogType type, B32 is_enabled);
internal B32 lnk_get_log_status(LNK_LogType type);
internal void lnk_log(LNK_LogType type, char *fmt, ...);
internal LNK_LogType lnk_log_type_from_string(String8 string);
+940
View File
@@ -0,0 +1,940 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
internal void
lnk_error_obj(LNK_ErrorCode code, LNK_Obj *obj, char *fmt, ...)
{
Temp scratch = scratch_begin(0, 0);
va_list args;
va_start(args, fmt);
String8 text = push_str8fv(scratch.arena, fmt, args);
if (obj->lib_path.size) {
lnk_error(code, "%S(%S): %S", obj->lib_path, obj->path, text);
} else {
lnk_error(code, "%S: %S", obj->path, text);
}
va_end(args);
scratch_end(scratch);
}
////////////////////////////////
internal void
lnk_input_obj_list_push_node(LNK_InputObjList *list, LNK_InputObj *node)
{
SLLQueuePush(list->first, list->last, node);
++list->count;
}
internal LNK_InputObj *
lnk_input_obj_list_push(Arena *arena, LNK_InputObjList *list)
{
LNK_InputObj *node = push_array(arena, LNK_InputObj, 1);
lnk_input_obj_list_push_node(list, node);
return node;
}
internal LNK_InputObj **
lnk_array_from_input_obj_list(Arena *arena, LNK_InputObjList list)
{
LNK_InputObj **result = push_array_no_zero(arena, LNK_InputObj *, list.count);
U64 i = 0;
for (LNK_InputObj *n = list.first; n != 0; n = n->next, ++i) {
Assert(i < list.count);
result[i] = n;
}
return result;
}
internal void
lnk_input_obj_list_concat_in_place(LNK_InputObjList *list, LNK_InputObjList *to_concat)
{
SLLConcatInPlace(list, to_concat);
}
internal int
lnk_input_obj_compar(const void *raw_a, const void *raw_b)
{
const LNK_InputObj **a = (const LNK_InputObj **) raw_a;
const LNK_InputObj **b = (const LNK_InputObj **) raw_b;
int cmp = str8_compar_case_sensetive(&(*a)->path, &(*b)->path);
return cmp;
}
internal int
lnk_input_obj_compar_is_before(void *raw_a, void *raw_b)
{
LNK_InputObj **a = raw_a;
LNK_InputObj **b = raw_b;
int cmp = str8_compar_case_sensetive(&(*a)->path, &(*b)->path);
int is_before = cmp < 0;
return is_before;
}
internal LNK_InputObjList
lnk_list_from_input_obj_arr(LNK_InputObj **arr, U64 count)
{
LNK_InputObjList list = {0};
for (U64 i = 0; i < count; ++i) {
SLLQueuePush(list.first, list.last, arr[i]);
++list.count;
}
return list;
}
internal LNK_InputObjList
lnk_input_obj_list_from_string_list(Arena *arena, String8List list)
{
LNK_InputObjList input_list = {0};
for (String8Node *path = list.first; path != 0; path = path->next) {
LNK_InputObj *input = lnk_input_obj_list_push(arena, &input_list);
input->is_thin = 1;
input->dedup_id = path->string;
input->path = path->string;
}
return input_list;
}
////////////////////////////////
internal LNK_Obj **
lnk_obj_arr_from_list(Arena *arena, LNK_ObjList list)
{
LNK_Obj **arr = push_array_no_zero(arena, LNK_Obj *, list.count);
U64 idx = 0;
for (LNK_ObjNode *node = list.first; node != 0; node = node->next, ++idx) {
arr[idx] = &node->data;
}
return arr;
}
internal LNK_ObjNodeArray
lnk_obj_list_reserve(Arena *arena, LNK_ObjList *list, U64 count)
{
LNK_ObjNodeArray arr = {0};
if (count) {
arr.count = count;
arr.v = push_array(arena, LNK_ObjNode, count);
for (LNK_ObjNode *ptr = arr.v, *opl = arr.v + arr.count; ptr < opl; ++ptr) {
SLLQueuePush(list->first, list->last, ptr);
}
list->count += count;
} else {
MemoryZeroStruct(&arr);
}
return arr;
}
internal LNK_ChunkList
lnk_obj_search_chunks(Arena *arena, LNK_Obj *obj, String8 name, String8 postfix, B32 collect_discarded)
{
LNK_ChunkList list = {0};
for (U64 sect_idx = 0; sect_idx < obj->chunk_count; ++sect_idx) {
String8 obj_sect_name = obj->sect_name_arr[sect_idx];
String8 obj_sect_sort = obj->sect_sort_arr[sect_idx];
B32 is_match = str8_match(obj_sect_name, name, 0) &&
str8_match(obj_sect_sort, postfix, 0);
if (is_match) {
LNK_ChunkPtr chunk = &obj->chunk_arr[sect_idx];
if (!collect_discarded && lnk_chunk_is_discarded(chunk)) {
continue;
}
LNK_ChunkNode *node = push_array_no_zero(arena, LNK_ChunkNode, 1);
node->next = 0;
node->data = chunk;
SLLQueuePush(list.first, list.last, node);
++list.count;
}
}
return list;
}
internal
THREAD_POOL_TASK_FUNC(lnk_collect_obj_chunks_task)
{
U64 obj_idx = task_id;
LNK_CollectObjChunksTaskData *task = raw_task;
LNK_Obj *obj = task->obj_arr[obj_idx];
LNK_ChunkList *list_ptr = &task->list_arr[obj_idx];
*list_ptr = lnk_obj_search_chunks(arena, obj, task->name, task->postfix, task->collect_discarded);
}
internal LNK_ChunkList *
lnk_collect_obj_chunks(TP_Context *tp, TP_Arena *arena, U64 obj_count, LNK_Obj **obj_arr, String8 name, String8 postfix, B32 collect_discarded)
{
LNK_CollectObjChunksTaskData task_data = {0};
task_data.obj_arr = obj_arr;
task_data.name = name;
task_data.postfix = postfix;
task_data.list_arr = push_array_no_zero(arena->v[0], LNK_ChunkList, obj_count);
task_data.collect_discarded = collect_discarded;
tp_for_parallel(tp, arena, obj_count, lnk_collect_obj_chunks_task, &task_data);
return task_data.list_arr;
}
internal
THREAD_POOL_TASK_FUNC(lnk_symbol_collector)
{
LNK_SymbolCollector *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
LNK_SymbolList *list = &task->out_arr[task_id];
for (U64 obj_idx = range.min; obj_idx < range.max; ++obj_idx) {
LNK_Obj *obj = &task->in_arr.v[obj_idx].data;
for (LNK_SymbolNode *node = obj->symbol_list.first; node != 0; node = node->next) {
if (node->data->type == task->type) {
lnk_symbol_list_push(arena, list, node->data);
}
}
}
}
internal LNK_SymbolList
lnk_run_symbol_collector(TP_Context *tp, TP_Arena *arena, LNK_ObjNodeArray arr, LNK_SymbolType symbol_type)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0,0);
LNK_SymbolCollector task_data;
task_data.type = symbol_type;
task_data.range_arr = tp_divide_work(scratch.arena, arr.count, tp->worker_count);
task_data.in_arr = arr;
task_data.out_arr = push_array(scratch.arena, LNK_SymbolList, tp->worker_count);
tp_for_parallel(tp, arena, tp->worker_count, lnk_symbol_collector, &task_data);
LNK_SymbolList list = {0};
for (U64 ithread = 0; ithread < tp->worker_count; ++ithread) {
lnk_symbol_list_concat_in_place(&list, &task_data.out_arr[ithread]);
}
scratch_end(scratch);
ProfEnd();
return list;
}
internal
THREAD_POOL_TASK_FUNC(lnk_default_lib_collector)
{
LNK_DefaultLibCollector *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
String8List *result = &task->out_arr[task_id];
for (U64 obj_idx = range.min; obj_idx < range.max; obj_idx += 1) {
LNK_Obj *obj = &task->in_arr.v[obj_idx].data;
String8List list = lnk_parse_default_lib_directive(arena, &obj->directive_info.v[LNK_Directive_DefaultLib]);
str8_list_concat_in_place(result, &list);
}
}
internal LNK_InputLibList
lnk_collect_default_lib_obj_arr(TP_Context *tp, TP_Arena *arena, LNK_ObjNodeArray arr)
{
Temp scratch = scratch_begin(0,0);
LNK_DefaultLibCollector task_data;
task_data.range_arr = tp_divide_work(scratch.arena, arr.count, tp->worker_count);
task_data.in_arr = arr;
task_data.out_arr = push_array(scratch.arena, LNK_InputLibList, tp->worker_count);
tp_for_parallel(tp, arena, tp->worker_count, lnk_default_lib_collector, &task_data);
String8List result = str8_list_arr_concat(task_data.out_arr, tp->worker_count);
scratch_end(scratch);
return result;
}
internal
THREAD_POOL_TASK_FUNC(lnk_manifest_dependency_collector)
{
LNK_ManifestDependencyCollector *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
String8List *list = &task->out_arr[task_id];
LNK_Obj **obj_ptr = &task->in_arr[range.min];
LNK_Obj **obj_opl = &task->in_arr[range.max];
for (; obj_ptr < obj_opl; obj_ptr += 1) {
LNK_Obj *obj = *obj_ptr;
LNK_DirectiveList *dirs = &obj->directive_info.v[LNK_Directive_ManifestDependency];
for (LNK_Directive *dir = dirs->first; dir != 0; dir = dir->next) {
String8List dep = str8_list_copy(arena, &dir->value_list);
str8_list_concat_in_place(list, &dep);
}
}
}
internal String8List
lnk_collect_manifest_dependency_list(TP_Context *tp, TP_Arena *arena, LNK_Obj **obj_arr, U64 obj_count)
{
Temp scratch = scratch_begin(0,0);
LNK_ManifestDependencyCollector task_data = {0};
task_data.in_arr = obj_arr;
task_data.out_arr = push_array(scratch.arena, String8List, tp->worker_count);
task_data.range_arr = tp_divide_work(scratch.arena, obj_count, tp->worker_count);
tp_for_parallel(tp, arena, tp->worker_count, lnk_manifest_dependency_collector, &task_data);
String8List result = str8_list_arr_concat(task_data.out_arr, tp->worker_count);
scratch_end(scratch);
return result;
}
internal void
lnk_sect_defn_list_push_node(LNK_SectDefnList *list, LNK_SectDefn *node)
{
SLLQueuePush(list->first, list->last, node);
++list->count;
}
internal LNK_SectDefn *
lnk_sect_defn_list_push(Arena *arena, LNK_SectDefnList *list, LNK_Obj *obj, String8 name, U64 idx, COFF_SectionFlags flags)
{
LNK_SectDefn *node = push_array_no_zero(arena, LNK_SectDefn, 1);
node->next = 0;
node->obj = obj;
node->name = name;
node->idx = idx;
node->flags = flags;
lnk_sect_defn_list_push_node(list, node);
return node;
}
internal void
lnk_sect_defn_list_concat_in_place(LNK_SectDefnList *list, LNK_SectDefnList *to_concat)
{
SLLConcatInPlace(list, to_concat);
}
internal void
lnk_sect_defn_list_concat_in_place_arr(LNK_SectDefnList *list, LNK_SectDefnList *to_concat_arr, U64 count)
{
SLLConcatInPlaceArray(list, to_concat_arr, count);
}
internal
THREAD_POOL_TASK_FUNC(lnk_obj_initer)
{
Temp scratch = scratch_begin(&arena, 1);
LNK_ObjIniter *task = raw_task;
LNK_InputObj *input = task->inputs[task_id];
U64 obj_idx = task->obj_id_base + task_id;
LNK_ObjNode *obj_node = task->obj_node_arr + task_id;
LNK_Obj *obj = &obj_node->data;
//Assert(coff_data.size > 0);
// cache path, we need it for error reports and debug stuff
String8 cached_path = push_str8_copy(arena, input->path);
String8 cached_lib_path = push_str8_copy(arena, input->lib_path);
// parse coff obj
COFF_HeaderInfo coff_info = coff_header_info_from_data(input->data);
COFF_SectionHeader *coff_sect_arr = (COFF_SectionHeader *)(input->data.str + coff_info.section_array_off);
COFF_Symbol32Array coff_symbols = coff_symbol_array_from_data(scratch.arena, input->data, coff_info.symbol_off, coff_info.symbol_count, coff_info.symbol_size);
// handle machines we dont support
if (coff_info.machine != COFF_MachineType_UNKNOWN &&
coff_info.machine != COFF_MachineType_X64) {
lnk_error(LNK_Error_UnsupportedMachine, "%S: %S machine is supported", input->path, coff_string_from_machine_type(coff_info.machine));
}
U64 chunk_count = 0;
chunk_count += coff_info.section_count_no_null;
chunk_count += 1; // :common_block
String8 *sect_name_arr = push_array_no_zero(arena, String8, chunk_count);
String8 *sect_sort_arr = push_array_no_zero(arena, String8, chunk_count);
LNK_Chunk *chunk_arr = push_array_no_zero(arena, LNK_Chunk, chunk_count);
// init section name and postfix array
for (U64 sect_idx = 0; sect_idx < coff_info.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *coff_sect = &coff_sect_arr[sect_idx];
// read name
String8 sect_name = coff_section_header_get_name(coff_sect, input->data, coff_info.string_table_off);
// parse section name
String8 name, postfix;
coff_parse_section_name(sect_name, &name, &postfix);
// fill out
sect_name_arr[sect_idx] = name;
sect_sort_arr[sect_idx] = postfix;
}
// :common_block
U64 common_block_idx = chunk_count - 1;
sect_name_arr[common_block_idx] = str8_lit(".bss");
sect_sort_arr[common_block_idx] = str8_lit("~");
for (U64 sect_idx = 0; sect_idx < coff_info.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *coff_sect = &coff_sect_arr[sect_idx];
String8 data;
if (coff_sect->flags & COFF_SectionFlag_CNT_UNINITIALIZED_DATA) {
data = str8(0, coff_sect->fsize);
} else {
data = str8(input->data.str + coff_sect->foff, coff_sect->fsize);
}
LNK_Chunk *chunk = &chunk_arr[sect_idx];
chunk->ref = lnk_chunk_ref(0,0); // :chunk_ref_assign
chunk->align = coff_align_size_from_section_flags(coff_sect->flags);
chunk->is_discarded = !!(coff_sect->flags & COFF_SectionFlag_LNK_REMOVE);
chunk->sort_chunk = 1;
chunk->type = LNK_Chunk_Leaf;
chunk->sort_idx = sect_sort_arr[sect_idx];
chunk->input_idx = LNK_MakeChunkInputIdx(obj_idx, sect_idx);
chunk->flags = coff_sect->flags;
chunk->associate = 0;
chunk->u.leaf = data;
lnk_chunk_set_debugf(arena, chunk, "%S: name: %S, isect: 0x%llX", path, sect_name_arr[sect_idx], sect_idx);
}
// :common_block
LNK_Chunk *master_common_block = &chunk_arr[common_block_idx];
master_common_block->ref = lnk_chunk_ref(0,0); // :chunk_ref_assign
master_common_block->align = 1;
master_common_block->is_discarded = 0;
master_common_block->sort_chunk = 0;
master_common_block->type = LNK_Chunk_List;
master_common_block->sort_idx = sect_sort_arr[common_block_idx];
master_common_block->input_idx = LNK_MakeChunkInputIdx(obj_idx, common_block_idx);
master_common_block->flags = LNK_BSS_SECTION_FLAGS;
master_common_block->associate = 0;
master_common_block->u.list = push_array(arena, LNK_ChunkList, 1);
lnk_chunk_set_debugf(arena, master_common_block, "%S: master common block", path);
// convert from coff
LNK_SymbolArray symbol_arr = lnk_symbol_array_from_coff(arena, input->data, cached_path, coff_info.string_table_off, coff_info.section_count_no_null, coff_sect_arr, coff_symbols, chunk_arr, master_common_block);
LNK_SymbolList symbol_list = lnk_symbol_list_from_array(arena, symbol_arr);
LNK_RelocList *reloc_list_arr = lnk_reloc_list_array_from_coff(arena, coff_info.machine, input->data, coff_info.section_count_no_null, coff_sect_arr, chunk_arr, symbol_arr);
// fill out obj
obj->data = input->data;
obj->path = cached_path;
obj->lib_path = cached_lib_path;
obj->machine = coff_info.machine;
obj->chunk_count = chunk_count;
obj->sect_count = coff_info.section_count_no_null;
obj->sect_name_arr = sect_name_arr;
obj->sect_sort_arr = sect_sort_arr;
obj->chunk_arr = chunk_arr;
obj->symbol_list = symbol_list;
obj->sect_reloc_list_arr = reloc_list_arr;
obj->directive_info = lnk_init_directives(arena, cached_path, coff_info.section_count_no_null, sect_name_arr, chunk_arr);
// parse exports
LNK_ExportParseList export_parse = {0};
for (LNK_Directive *dir = obj->directive_info.v[LNK_Directive_Export].first; dir != 0; dir = dir->next) {
lnk_parse_export_direcive(arena, &obj->export_parse, dir->value_list, obj);
}
// push /export symbols
for (LNK_ExportParse *exp = export_parse.first; exp != 0; exp = exp->next) {
LNK_Symbol *symbol = lnk_make_undefined_symbol(arena, exp->name, LNK_SymbolScopeFlag_Main);
lnk_symbol_list_push(arena, &obj->symbol_list, symbol);
}
// push /include symbols
for (LNK_Directive *dir = obj->directive_info.v[LNK_Directive_Include].first; dir != 0; dir = dir->next) {
str8_list_concat_in_place(&obj->include_symbol_list, &dir->value_list);
}
// parse /alternatename
for (LNK_Directive *dir = obj->directive_info.v[LNK_Directive_AlternateName].first; dir != 0; dir = dir->next) {
String8 *invalid_string = lnk_parse_alt_name_directive_list(arena, dir->value_list, &obj->alt_name_list);
if (invalid_string != 0) {
lnk_error_obj(LNK_Error_Cmdl, obj, "invalid syntax \"%S\", expected format \"FROM=TO\"", *invalid_string);
}
}
scratch_end(scratch);
}
internal
THREAD_POOL_TASK_FUNC(lnk_obj_new_sect_scanner)
{
LNK_ObjNewSectScanner *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
HashTable *ht = hash_table_init(arena, 128);
for (U64 obj_idx = range.min; obj_idx < range.max; obj_idx += 1) {
LNK_Obj *obj = &task->obj_node_arr[obj_idx].data;
for (U64 chunk_idx = 0; chunk_idx < obj->chunk_count; chunk_idx += 1) {
String8 sect_name = obj->sect_name_arr[chunk_idx];
COFF_SectionFlags sect_flags = obj->chunk_arr[chunk_idx].flags & ~COFF_SectionFlags_LNK_FLAGS;
KeyValuePair *is_present = hash_table_search_string(ht, sect_name);
if (is_present) {
if (lnk_is_error_code_active(LNK_Warning_SectionFlagsConflict)) {
LNK_SectDefn *defn = is_present->value_raw;
if (defn->flags != sect_flags) {
lnk_sect_defn_list_push(arena, &task->defn_arr[task_id], obj, sect_name, chunk_idx, sect_flags);
}
}
} else {
LNK_SectDefn *defn = lnk_sect_defn_list_push(arena, &task->defn_arr[task_id], obj, sect_name, chunk_idx, sect_flags);
hash_table_push_string_raw(arena, ht, sect_name, defn);
}
}
}
}
LNK_CHUNK_VISITOR_SIG(lnk_chunk_get_count_cb)
{
U64 *counter = (U64 *)ud;
*counter += 1;
return 0;
}
internal
THREAD_POOL_TASK_FUNC(lnk_chunk_counter)
{
U64 obj_idx = task_id;
LNK_ChunkCounter *task = raw_task;
LNK_Obj *obj = &task->obj_arr[obj_idx].data;
for (U64 chunk_idx = 0; chunk_idx < obj->chunk_count; chunk_idx += 1) {
String8 name = obj->sect_name_arr[chunk_idx];
LNK_Chunk *chunk = &obj->chunk_arr[chunk_idx];
LNK_Section *sect = lnk_section_table_search(task->st, name);
U64 count = 0;
lnk_visit_chunks(0, chunk, lnk_chunk_get_count_cb, &count);
task->chunk_count_arr_arr[sect->id][obj_idx] += count;
}
}
internal
LNK_CHUNK_VISITOR_SIG(lnk_chunk_ref_assign)
{
LNK_ChunkRefAssign *ctx = ud;
// alloc chunk id
U64 chunk_id = ctx->chunk_id_arr_arr[sect_id][ctx->obj_idx];
ctx->chunk_id_arr_arr[sect_id][ctx->obj_idx] += 1;
// set chunk ref
chunk->ref = lnk_chunk_ref(sect_id, chunk_id);
// keep visiting chunks
return 0;
}
internal
THREAD_POOL_TASK_FUNC(lnk_chunk_ref_assigner)
{
LNK_ChunkRefAssigner *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
for (U64 obj_idx = range.min; obj_idx < range.max; obj_idx += 1) {
LNK_Obj *obj = &task->obj_arr[obj_idx].data;
for (U64 chunk_idx = 0; chunk_idx < obj->chunk_count; chunk_idx += 1) {
String8 name = obj->sect_name_arr[chunk_idx];
String8 sort = obj->sect_sort_arr[chunk_idx];
LNK_Chunk *chunk = &obj->chunk_arr[chunk_idx];
// :find_chunk_section
LNK_Section *sect = lnk_section_table_search(task->st, name);
Assert(sect);
// :chunk_ref_assign
LNK_ChunkRefAssign ctx;
ctx.cman = sect->cman;
ctx.chunk_id_arr_arr = task->chunk_id_arr_arr;
ctx.obj_idx = obj_idx;
lnk_visit_chunks(sect->id, chunk, lnk_chunk_ref_assign, &ctx);
// push to section chunk list
LNK_ChunkList **chunk_list_arr_arr = sort.size ? task->chunk_list_arr_arr : task->nosort_chunk_list_arr_arr;
lnk_chunk_list_push(arena, &chunk_list_arr_arr[sect->id][task_id], chunk);
}
}
}
internal LNK_ObjNodeArray
lnk_obj_list_push_parallel(TP_Context *tp, TP_Arena *arena, LNK_ObjList *obj_list, LNK_SectionTable *st, U64 input_count, LNK_InputObj **inputs)
{
ProfBeginFunction();
Temp scratch = scratch_begin(arena->v, arena->count);
U64 obj_id_base = obj_list->count;
LNK_ObjNodeArray obj_arr = lnk_obj_list_reserve(arena->v[0], obj_list, input_count);
ProfBegin("Obj Initer");
{
LNK_ObjIniter task = {0};
task.inputs = inputs;
task.obj_id_base = obj_id_base;
task.obj_node_arr = obj_arr.v;
tp_for_parallel(tp, arena, input_count, lnk_obj_initer, &task);
}
ProfEnd();
if (st) {
ProfBegin("Section Table Update");
{
TP_Temp temp = tp_temp_begin(arena);
LNK_ObjNewSectScanner task;
task.range_arr = tp_divide_work(arena->v[0], obj_arr.count, tp->worker_count);
task.obj_node_arr = obj_arr.v;
task.defn_arr = push_array(arena->v[0], LNK_SectDefnList, tp->worker_count);
task.conf_arr = push_array(arena->v[0], LNK_SectDefnList, tp->worker_count);
tp_for_parallel(tp, arena, tp->worker_count, lnk_obj_new_sect_scanner, &task);
LNK_SectDefnList defn_list = {0};
LNK_SectDefnList conf_list = {0};
lnk_sect_defn_list_concat_in_place_arr(&defn_list, task.defn_arr, tp->worker_count);
lnk_sect_defn_list_concat_in_place_arr(&conf_list, task.conf_arr, tp->worker_count);
HashTable *ht = hash_table_init(arena->v[0], 128);
for (LNK_SectionNode *sect_node = st->list.first; sect_node != 0; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
hash_table_push_string_u64(arena->v[0], ht, sect->name, sect->flags);
}
LNK_SectDefnList new_list = {0};
for (LNK_SectDefn *curr = defn_list.first, *next; curr != 0; curr = next) {
next = curr->next;
curr->next = 0;
KeyValuePair *is_present = hash_table_search_string(ht, curr->name);
if (is_present) {
if (lnk_is_error_code_active(LNK_Warning_SectionFlagsConflict)) {
COFF_SectionFlags flags = is_present->value_u64;
if (flags != curr->flags) {
lnk_sect_defn_list_push_node(&conf_list, curr);
} else {
// section is present or is in new_list
}
}
} else {
lnk_sect_defn_list_push_node(&new_list, curr);
hash_table_push_string_u64(arena->v[0], ht, curr->name, curr->flags);
}
}
for (LNK_SectDefn *defn = conf_list.first; defn != 0; defn = defn->next) {
KeyValuePair *is_present = hash_table_search_string(ht, defn->name);
if (!is_present) {
InvalidPath;
}
U64 sect_number = (defn->idx + 1);
COFF_SectionFlags expected_flags = is_present->value_u64;
String8 expected_flags_str = coff_string_from_section_flags(scratch.arena, expected_flags);
String8 current_flags_str = coff_string_from_section_flags(scratch.arena, defn->flags);
lnk_error_obj(LNK_Warning_SectionFlagsConflict, defn->obj, "detected section flags conflict in %S(No. %X); expected {%S} but got {%S}", defn->name, sect_number, expected_flags_str, current_flags_str);
}
// push new sections for :find_chunk_section
for (LNK_SectDefn *curr = new_list.first; curr != 0; curr = curr->next) {
lnk_section_table_push(st, curr->name, curr->flags & ~COFF_SectionFlags_LNK_FLAGS);
}
tp_temp_end(temp);
}
ProfEnd();
ProfBegin("Count Chunks Per Section");
U64 **chunk_id_arr_arr;
{
U64 **chunk_count_arr_arr = push_array_no_zero(scratch.arena, U64 *, st->id_max);
for (U64 sect_idx = 0; sect_idx < st->id_max; sect_idx += 1) {
chunk_count_arr_arr[sect_idx] = push_array(scratch.arena, U64, obj_arr.count);
}
LNK_ChunkCounter task;
task.st = st;
task.obj_arr = obj_arr.v;
task.chunk_count_arr_arr = chunk_count_arr_arr;
tp_for_parallel(tp, 0, obj_arr.count, lnk_chunk_counter, &task);
chunk_id_arr_arr = chunk_count_arr_arr;
for (U64 sect_idx = 1; sect_idx < st->id_max; sect_idx += 1) {
LNK_Section *sect = lnk_section_table_search_id(st, sect_idx);
if (!sect) continue;
for (U64 obj_idx = 0; obj_idx < obj_arr.count; obj_idx += 1) {
U64 chunk_id_base = sect->cman->total_chunk_count;
sect->cman->total_chunk_count += chunk_count_arr_arr[sect_idx][obj_idx];
chunk_id_arr_arr[sect_idx][obj_idx] = chunk_id_base;
}
}
}
ProfEnd();
ProfBegin("Assign Chunk Refs");
{
LNK_ChunkRefAssigner task;
task.st = st;
task.range_arr = tp_divide_work(scratch.arena, obj_arr.count, tp->worker_count);
task.chunk_id_arr_arr = chunk_id_arr_arr;
task.obj_arr = obj_arr.v;
task.nosort_chunk_list_arr_arr = lnk_make_chunk_list_arr_arr(scratch.arena, st->id_max, tp->worker_count);
task.chunk_list_arr_arr = lnk_make_chunk_list_arr_arr(scratch.arena, st->id_max, tp->worker_count);
tp_for_parallel(tp, arena, tp->worker_count, lnk_chunk_ref_assigner, &task);
// merge chunks
for (LNK_SectionNode *sect_node = st->list.first; sect_node != 0; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
lnk_chunk_list_concat_in_place_arr(sect->nosort_chunk->u.list, task.nosort_chunk_list_arr_arr[sect->id], tp->worker_count);
lnk_chunk_list_concat_in_place_arr(sect->root->u.list, task.chunk_list_arr_arr[sect->id], tp->worker_count);
}
}
ProfEnd();
}
ProfEnd();
scratch_end(scratch);
return obj_arr;
}
internal LNK_SymbolArray
lnk_symbol_array_from_coff(Arena *arena,
String8 coff_data,
String8 obj_path,
U64 string_table_off,
U64 sect_count,
COFF_SectionHeader *coff_sect_arr,
COFF_Symbol32Array coff_symbols,
LNK_Chunk *chunk_arr,
LNK_Chunk *master_common_block)
{
LNK_SymbolList weak_symbol_list = {0};
LNK_SymbolArray symbol_array = {0};
symbol_array.count = coff_symbols.count;
symbol_array.v = push_array(arena, LNK_Symbol, symbol_array.count);
for (U64 symbol_idx = 0; symbol_idx < coff_symbols.count; symbol_idx += 1) {
COFF_Symbol32 *coff_symbol = &coff_symbols.v[symbol_idx];
LNK_Symbol *symbol = &symbol_array.v[symbol_idx];
lnk_symbol_set_debug(symbol, obj_path);
String8 name = coff_read_symbol_name(coff_data, string_table_off, &coff_symbol->name);
// TODO: we convert 16-bit symbols and copy them to arena; symbols with short names
// are stored in the symbol itself and becuase converted symbols are pushed to scratch
// that memory is discarded after obj is processed
name = push_str8_copy(arena, name);
COFF_SymbolValueInterpType interp = coff_interp_symbol(coff_symbol);
switch (interp) {
case COFF_SymbolValueInterp_REGULAR: {
if (coff_symbol->section_number == 0 || coff_symbol->section_number > sect_count) {
lnk_error(LNK_Error_IllData, "%S: out ouf bounds section index in symbol \"%S (%u)\"", obj_path, name, coff_symbol->section_number);
break;
}
LNK_DefinedSymbolVisibility visibility = LNK_DefinedSymbolVisibility_Static;
if (coff_symbol->storage_class == COFF_SymStorageClass_EXTERNAL) {
visibility = LNK_DefinedSymbolVisibility_Extern;
}
LNK_DefinedSymbolFlags flags = 0;
if (coff_symbol->type.u.lsb == COFF_SymType_NULL && coff_symbol->type.u.msb == COFF_SymDType_FUNC) {
flags |= LNK_DefinedSymbolFlag_IsFunc;
}
COFF_ComdatSelectType selection = COFF_ComdatSelectType_ANY;
U64 check_sum = 0;
{
B32 is_comdat = !!(coff_sect_arr[coff_symbol->section_number - 1].flags & COFF_SectionFlag_LNK_COMDAT);
B32 has_static_def = is_comdat &&
coff_symbol->value == 0 &&
coff_symbol->type.u.lsb == COFF_SymType_NULL &&
coff_symbol->storage_class == COFF_SymStorageClass_STATIC &&
coff_symbol->aux_symbol_count == 1;
if (has_static_def) {
COFF_SymbolSecDef *secdef = (COFF_SymbolSecDef *)(coff_symbol + 1);
selection = secdef->selection;
check_sum = secdef->check_sum;
if (secdef->selection == COFF_ComdatSelectType_ASSOCIATIVE) {
LNK_Chunk *head_chunk = &chunk_arr[secdef->number - 1];
LNK_Chunk *associate_chunk = &chunk_arr[coff_symbol->section_number - 1];
lnk_chunk_associate(arena, head_chunk, associate_chunk);
}
}
}
LNK_Chunk *chunk = &chunk_arr[coff_symbol->section_number - 1];
U64 offset = coff_symbol->value;
lnk_init_defined_symbol_chunk(symbol, name, visibility, flags, chunk, offset, selection, check_sum);
} break;
case COFF_SymbolValueInterp_UNDEFINED: {
lnk_init_undefined_symbol(symbol, name, LNK_SymbolScopeFlag_Main);
} break;
case COFF_SymbolValueInterp_COMMON: {
// :common_block
LNK_Chunk *chunk = push_array_no_zero(arena, LNK_Chunk, 1);
chunk->ref = lnk_chunk_ref(0,0); // :chunk_ref_assign
chunk->align = 1;
chunk->is_discarded = 0;
chunk->sort_chunk = 1;
chunk->type = LNK_Chunk_Leaf;
chunk->sort_idx = str8(0,0);
chunk->input_idx = LNK_MakeChunkInputIdx(0, lnk_chunk_list_get_node_count(master_common_block));
chunk->flags = LNK_BSS_SECTION_FLAGS;
chunk->associate = 0;
chunk->u.leaf = str8(0, coff_symbol->value);
lnk_chunk_set_debugf(arena, chunk, "common block %S", name);
lnk_chunk_list_push(arena, master_common_block->u.list, chunk);
LNK_DefinedSymbolVisibility visibility = LNK_DefinedSymbolVisibility_Extern;
LNK_DefinedSymbolFlags flags = 0;
if (coff_symbol->type.u.lsb == COFF_SymType_NULL && coff_symbol->type.u.msb == COFF_SymDType_FUNC) {
flags |= LNK_DefinedSymbolFlag_IsFunc;
}
lnk_init_defined_symbol_chunk(symbol, name, visibility, flags, chunk, 0, COFF_ComdatSelectType_LARGEST, 0);
} break;
case COFF_SymbolValueInterp_WEAK: {
if (coff_symbol->aux_symbol_count == 0 || symbol_idx + 1 >= coff_symbols.count) {
lnk_error(LNK_Error_IllData, "%S: Weak symbol \"%S (%u)\" must at least one aux symbol", obj_path, name, symbol_idx);
break;
}
COFF_SymbolWeakExt *weak_ext = (COFF_SymbolWeakExt*)(coff_symbol + 1);
if (weak_ext->tag_index >= symbol_array.count) {
lnk_error(LNK_Error_IllData, "%S: Weak symbol \"%S (%u)\" points to out of bounds symbol", obj_path, name, symbol_idx);
break;
}
#if 0
if (symbol_array.v[weak_ext->tag_index] == NULL) {
lnk_error(LNK_ERROR_ILL_DATA, "%S: Weak symbol \"%S (%u)\" tags auxiliary symbol %u",
obj_path, name, symbol_idx, weak_ext->tag_index);
break;
}
#endif
lnk_init_weak_symbol(symbol, name, weak_ext->characteristics, &symbol_array.v[weak_ext->tag_index]);
lnk_symbol_list_push(arena, &weak_symbol_list, symbol);
} break;
case COFF_SymbolValueInterp_ABS: {
// Never code or data, synthetic symbol. COFF spec says bits in value are used
// as flags in symbol @feat.00, other symbols like @comp.id and @vol.md are undocumented.
// LLVM uses undocumented mask 0x4800 on @feat.00 to tell if object was compiled with /guard:cf.
LNK_DefinedSymbolVisibility visibility = LNK_DefinedSymbolVisibility_Static;
if (coff_symbol->storage_class == COFF_SymStorageClass_EXTERNAL) {
visibility = LNK_DefinedSymbolVisibility_Extern;
}
lnk_init_defined_symbol_va(symbol, name, visibility, 0, coff_symbol->value);
} break;
case COFF_SymbolValueInterp_DEBUG: {
// ignore
} break;
}
// skip aux symbols
symbol_idx += coff_symbol->aux_symbol_count;
}
return symbol_array;
}
internal LNK_RelocList *
lnk_reloc_list_array_from_coff(Arena *arena, COFF_MachineType machine, String8 coff_data, U64 sect_count, COFF_SectionHeader *coff_sect_arr, LNK_Chunk *chunk_arr, LNK_SymbolArray symbol_array)
{
LNK_RelocList *reloc_list_arr = push_array_no_zero(arena, LNK_RelocList, sect_count);
for (U64 sect_idx = 0; sect_idx < sect_count; sect_idx += 1) {
COFF_SectionHeader *coff_header = &coff_sect_arr[sect_idx];
COFF_RelocInfo coff_reloc_info = coff_reloc_info_from_section_header(coff_data, coff_header);
COFF_Reloc *coff_reloc_v = (COFF_Reloc *)(coff_data.str + coff_reloc_info.array_off);
LNK_Chunk *sect_chunk = &chunk_arr[sect_idx];
reloc_list_arr[sect_idx] = lnk_reloc_list_from_coff_reloc_array(arena, machine, sect_chunk, symbol_array, coff_reloc_v, coff_reloc_info.count);
}
return reloc_list_arr;
}
internal LNK_DirectiveInfo
lnk_init_directives(Arena *arena, String8 obj_path, U64 chunk_count, String8 *sect_name_arr, LNK_Chunk *chunk_arr)
{
LNK_DirectiveInfo directive_info = {0};
for (U64 chunk_idx = 0; chunk_idx < chunk_count; chunk_idx += 1) {
String8 sect_name = sect_name_arr[chunk_idx];
LNK_Chunk *sect_chunk = &chunk_arr[chunk_idx];
Assert(sect_chunk->type == LNK_Chunk_Leaf);
if (!str8_match(sect_name, str8_lit(".drectve"), 0)) {
continue;
}
if (sect_chunk->u.leaf.size < 3) {
lnk_error(LNK_Warning_IllData, "%S: can't parse %S", obj_path, sect_name);
continue;
}
if (~sect_chunk->flags & COFF_SectionFlag_LNK_INFO) {
lnk_error(LNK_Warning_IllData, "%S: %S missing COFF_SectionFlag_LNK_INFO.", obj_path, sect_name);
}
// TODO: warn if section has relocations
lnk_parse_directives(arena, &directive_info, sect_chunk->u.leaf, obj_path);
int bad_vs = 0; (void)bad_vs;
}
return directive_info;
}
internal COFF_FeatFlags
lnk_obj_get_features(LNK_Obj *obj)
{
COFF_FeatFlags result = 0;
LNK_Symbol *sym = lnk_symbol_list_search(obj->symbol_list, str8_lit("@feat.00"), 0);
if (sym) {
Assert(LNK_Symbol_IsDefined(sym->type));
Assert(sym->u.defined.value_type == LNK_DefinedSymbolValue_VA);
result = sym->u.defined.u.va;
}
return result;
}
internal U32
lnk_obj_get_comp_id(LNK_Obj *obj)
{
U32 result = 0;
LNK_Symbol *sym = lnk_symbol_list_search(obj->symbol_list, str8_lit("@comp.id"), 0);
if (sym) {
Assert(LNK_Symbol_IsDefined(sym->type));
Assert(sym->u.defined.value_type == LNK_DefinedSymbolValue_VA);
result = sym->u.defined.u.va;
}
return result;
}
internal U32
lnk_obj_get_vol_md(LNK_Obj *obj)
{
U32 result = 0;
LNK_Symbol *sym = lnk_symbol_list_search(obj->symbol_list, str8_lit("@vol.md"), 0);
if (sym) {
Assert(LNK_Symbol_IsDefined(sym->type));
Assert(sym->u.defined.value_type == LNK_DefinedSymbolValue_VA);
result = sym->u.defined.u.va;
}
return result;
}
+190
View File
@@ -0,0 +1,190 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
////////////////////////////////
typedef struct LNK_InputObj
{
struct LNK_InputObj *next;
B32 is_thin;
B32 has_disk_read_failed;
String8 dedup_id;
String8 path;
String8 data;
String8 lib_path;
} LNK_InputObj;
typedef struct LNK_InputObjList
{
U64 count;
LNK_InputObj *first;
LNK_InputObj *last;
} LNK_InputObjList;
////////////////////////////////
#define LNK_MakeChunkInputIdx(obj_idx, sect_idx) (((U64)(obj_idx) << 32) | (U64)((sect_idx) & max_U32))
typedef struct LNK_Obj
{
String8 data;
String8 path;
String8 lib_path;
U64 common_symbol_size;
COFF_MachineType machine;
U64 chunk_count;
U64 sect_count;
String8 *sect_name_arr;
String8 *sect_sort_arr;
LNK_RelocList *sect_reloc_list_arr;
LNK_Chunk *chunk_arr;
LNK_SymbolList symbol_list;
LNK_DirectiveInfo directive_info;
LNK_ExportParseList export_parse;
String8List include_symbol_list;
LNK_AltNameList alt_name_list;
} LNK_Obj;
typedef struct LNK_ObjNode
{
struct LNK_ObjNode *next;
LNK_Obj data;
} LNK_ObjNode;
typedef struct LNK_ObjList
{
U64 count;
LNK_ObjNode *first;
LNK_ObjNode *last;
} LNK_ObjList;
typedef struct LNK_ObjNodeArray
{
U64 count;
LNK_ObjNode *v;
} LNK_ObjNodeArray;
////////////////////////////////
typedef struct LNK_SectDefn
{
struct LNK_SectDefn *next;
LNK_Obj *obj;
String8 name;
COFF_SectionFlags flags;
U64 idx;
} LNK_SectDefn;
typedef struct
{
U64 count;
LNK_SectDefn *first;
LNK_SectDefn *last;
} LNK_SectDefnList;
typedef struct
{
LNK_InputObj **inputs;
LNK_ObjNode *obj_node_arr;
U64 obj_id_base;
LNK_SectDefnList *defn_arr;
LNK_SectionTable *st;
} LNK_ObjIniter;
typedef struct
{
Rng1U64 *range_arr;
LNK_ObjNode *obj_node_arr;
LNK_SectDefnList *defn_arr;
LNK_SectDefnList *conf_arr;
} LNK_ObjNewSectScanner;
typedef struct
{
LNK_SectionTable *st;
LNK_ObjNode *obj_arr;
U64 **chunk_count_arr_arr;
} LNK_ChunkCounter;
typedef struct
{
LNK_ChunkManager *cman;
U64 **chunk_id_arr_arr;
U64 obj_idx;
} LNK_ChunkRefAssign;
typedef struct
{
LNK_SectionTable *st;
Rng1U64 *range_arr;
U64 **chunk_id_arr_arr;
LNK_ObjNode *obj_arr;
LNK_ChunkList **nosort_chunk_list_arr_arr;
LNK_ChunkList **chunk_list_arr_arr;
} LNK_ChunkRefAssigner;
typedef struct
{
LNK_SymbolType type;
LNK_ObjNodeArray in_arr;
LNK_SymbolList *out_arr;
Rng1U64 *range_arr;
} LNK_SymbolCollector;
typedef struct
{
LNK_Obj **obj_arr;
String8 name;
String8 postfix;
B32 collect_discarded;
LNK_ChunkList *list_arr;
} LNK_CollectObjChunksTaskData;
typedef struct
{
Rng1U64 *range_arr;
LNK_ObjNodeArray in_arr;
String8List *out_arr;
} LNK_DefaultLibCollector;
typedef struct
{
LNK_Obj **in_arr;
String8List *out_arr;
Rng1U64 *range_arr;
} LNK_ManifestDependencyCollector;
////////////////////////////////
internal void lnk_error_obj(LNK_ErrorCode code, LNK_Obj *obj, char *fmt, ...);
////////////////////////////////
internal void lnk_input_obj_list_push_node(LNK_InputObjList *list, LNK_InputObj *node);
internal void lnk_input_obj_list_concat_in_place(LNK_InputObjList *list, LNK_InputObjList *to_concat);
internal LNK_InputObj * lnk_input_obj_list_push(Arena *arena, LNK_InputObjList *list);
internal LNK_InputObj ** lnk_array_from_input_obj_list(Arena *arena, LNK_InputObjList list);
internal LNK_InputObjList lnk_input_obj_list_from_string_list(Arena *arena, String8List list);
internal LNK_InputObjList lnk_list_from_input_obj_arr(LNK_InputObj **arr, U64 count);
////////////////////////////////
internal LNK_InputObjList lnk_input_obj_list_from_string_list(Arena *arena, String8List list);
internal LNK_Obj ** lnk_obj_arr_from_list(Arena *arena, LNK_ObjList list);
internal LNK_ObjNodeArray lnk_obj_list_reserve(Arena *arena, LNK_ObjList *list, U64 count);
internal LNK_ChunkList * lnk_collect_obj_chunks(TP_Context *tp, TP_Arena *arena, U64 obj_count, LNK_Obj **obj_arr, String8 name, String8 postfix, B32 collect_discarded);
internal LNK_ObjNodeArray lnk_obj_list_push_parallel(TP_Context *tp, TP_Arena *tp_arena, LNK_ObjList *obj_list, LNK_SectionTable *st, U64 input_count, LNK_InputObj **inputs);
internal LNK_Chunk * lnk_sect_chunk_array_from_coff(Arena *arena, U64 obj_id, String8 obj_path, String8 coff_data, U64 sect_count, COFF_SectionHeader *coff_sect_arr, String8 *sect_name_arr, String8 *sect_postfix_arr);
internal LNK_SymbolArray lnk_symbol_array_from_coff(Arena *arena, String8 coff_data, String8 obj_path, U64 string_table_off, U64 sect_count, COFF_SectionHeader *coff_sect_arr, COFF_Symbol32Array coff_symbols, LNK_Chunk *chunk_arr, LNK_Chunk *master_common_block);
internal LNK_RelocList lnk_reloc_list_from_coff_reloc_array(Arena *arena, COFF_MachineType machine, LNK_Chunk *chunk, LNK_SymbolArray symbol_array, COFF_Reloc *reloc_v, U64 reloc_count);
internal LNK_RelocList * lnk_reloc_list_array_from_coff(Arena *arena, COFF_MachineType machine, String8 coff_data, U64 sect_count, COFF_SectionHeader *coff_sect_arr, LNK_Chunk *sect_chunk_arr, LNK_SymbolArray symbol_array);
internal LNK_DirectiveInfo lnk_init_directives(Arena *arena, String8 obj_path, U64 chunk_count, String8 *sect_name_arr, LNK_Chunk *chunk_arr);
internal U32 lnk_obj_get_features(LNK_Obj *obj);
internal U32 lnk_obj_get_comp_id(LNK_Obj *obj);
internal U32 lnk_obj_get_vol_md(LNK_Obj *obj);
+153
View File
@@ -0,0 +1,153 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal LNK_Reloc *
lnk_reloc_list_reserve(Arena *arena, LNK_RelocList *list, U64 count)
{
LNK_Reloc *arr = NULL;
if (count) {
arr = push_array(arena, LNK_Reloc, count);
for (LNK_Reloc *ptr = arr, *opl = arr + count; ptr < opl; ++ptr) {
SLLQueuePush(list->first, list->last, ptr);
}
list->count += count;
}
return arr;
}
internal LNK_Reloc *
lnk_reloc_list_push(Arena *arena, LNK_RelocList *list)
{
LNK_Reloc *node = push_array(arena, LNK_Reloc, 1);
SLLQueuePush(list->first, list->last, node);
list->count += 1;
return node;
}
internal LNK_RelocList
lnk_reloc_list_copy(Arena *arena, LNK_RelocList *list)
{
LNK_RelocList result = {0};
for (LNK_Reloc *n = list->first; n != NULL; n = n->next) {
LNK_Reloc *r = lnk_reloc_list_push(arena, &result);
r->chunk = n->chunk;
r->type = n->type;
r->apply_off = n->apply_off;
r->symbol = n->symbol;
}
return result;
}
internal void
lnk_reloc_list_concat_in_place(LNK_RelocList *list, LNK_RelocList *to_concat)
{
SLLConcatInPlace(list, to_concat);
}
internal void
lnk_reloc_list_concat_in_place_arr(LNK_RelocList *list, LNK_RelocList *arr, U64 count)
{
SLLConcatInPlaceArray(list, arr, count);
}
internal LNK_RelocList **
lnk_make_reloc_list_arr_arr(Arena *arena, U64 slot_count, U64 per_count)
{
LNK_RelocList **arr_arr = push_array_no_zero(arena, LNK_RelocList *, slot_count);
for (U64 i = 0; i < slot_count; i += 1) {
arr_arr[i] = push_array(arena, LNK_RelocList, per_count);
}
return arr_arr;
}
internal LNK_RelocList
lnk_reloc_list_from_coff_reloc_array(Arena *arena, COFF_MachineType machine, LNK_Chunk *chunk, LNK_SymbolArray symbol_array, COFF_Reloc *reloc_v, U64 reloc_count)
{
LNK_RelocList reloc_list = {0};
LNK_Reloc *reloc_arr = lnk_reloc_list_reserve(arena, &reloc_list, reloc_count);
LNK_Reloc *reloc_ptr = reloc_arr;
LNK_Reloc *reloc_opl = reloc_arr + reloc_count;
COFF_Reloc *coff_reloc_ptr = reloc_v;
for (; reloc_ptr < reloc_opl; reloc_ptr += 1, coff_reloc_ptr += 1) {
Assert(coff_reloc_ptr->isymbol < symbol_array.count);
reloc_ptr->chunk = chunk;
reloc_ptr->type = lnk_ext_reloc_type_from_coff(machine, coff_reloc_ptr->type);
reloc_ptr->apply_off = coff_reloc_ptr->apply_off;
reloc_ptr->symbol = symbol_array.v + coff_reloc_ptr->isymbol;
}
return reloc_list;
}
internal LNK_Reloc **
lnk_reloc_array_from_list(Arena *arena, LNK_RelocList list)
{
LNK_Reloc **arr = push_array_no_zero(arena, LNK_Reloc *, list.count);
U64 count = 0;
for (LNK_Reloc *node = list.first; node != 0; node = node->next) {
Assert(count < list.count);
arr[count++] = node;
}
return arr;
}
internal LNK_RelocType
lnk_ext_reloc_type_from_coff(COFF_MachineType machine, U32 type)
{
LNK_RelocType result = LNK_Reloc_NULL;
switch (machine) {
case COFF_MachineType_UNKNOWN: break;
case COFF_MachineType_X64: {
switch (type) {
case COFF_RelocTypeX64_ABS: result = LNK_Reloc_NULL; break;
case COFF_RelocTypeX64_ADDR64: result = LNK_Reloc_ADDR_64; break;
case COFF_RelocTypeX64_ADDR32: result = LNK_Reloc_ADDR_32; break;
case COFF_RelocTypeX64_ADDR32NB: result = LNK_Reloc_VIRT_OFF_32; break;
case COFF_RelocTypeX64_REL32: result = LNK_Reloc_REL32; break;
case COFF_RelocTypeX64_REL32_1: result = LNK_Reloc_REL32_1; break;
case COFF_RelocTypeX64_REL32_2: result = LNK_Reloc_REL32_2; break;
case COFF_RelocTypeX64_REL32_3: result = LNK_Reloc_REL32_3; break;
case COFF_RelocTypeX64_REL32_4: result = LNK_Reloc_REL32_4; break;
case COFF_RelocTypeX64_REL32_5: result = LNK_Reloc_REL32_5; break;
case COFF_RelocTypeX64_SECTION: result = LNK_Reloc_SECT_IDX; break;
case COFF_RelocTypeX64_SECREL: result = LNK_Reloc_SECT_REL; break;
case COFF_RelocTypeX64_SECREL7: lnk_not_implemented("TODO: COFF_RelocTypeX64_SECREL7"); break;
case COFF_RelocTypeX64_TOKEN: lnk_not_implemented("TODO: COFF_RelocTypeX64_TOKEN"); break;
case COFF_RelocTypeX64_SREL32: lnk_not_implemented("TODO: COFF_RelocTypeX64_SREL32"); break;
case COFF_RelocTypeX64_PAIR: lnk_not_implemented("TODO: COFF_RelocTypeX64_PAIR"); break;
case COFF_RelocTypeX64_SSPAN32: lnk_not_implemented("TODO: COFF_RelocTypeX64_SSPAN32"); break;
default: lnk_invalid_path("unknown relocation type 0x%X", type);
}
} break;
default: lnk_not_implemented("TODO: define remap for coff reloc types"); break;
}
return result;
}
internal U32
lnk_ext_reloc_type_to_coff(COFF_MachineType machine, LNK_RelocType type)
{
U32 result = 0;
switch (machine) {
case COFF_MachineType_X64: {
switch (type) {
case LNK_Reloc_NULL: result = COFF_RelocTypeX64_ABS; break;
case LNK_Reloc_ADDR_64: result = COFF_RelocTypeX64_ADDR64; break;
case LNK_Reloc_ADDR_32: result = COFF_RelocTypeX64_ADDR32; break;
case LNK_Reloc_VIRT_OFF_32: result = COFF_RelocTypeX64_ADDR32NB; break;
case LNK_Reloc_REL32: result = COFF_RelocTypeX64_REL32; break;
case LNK_Reloc_REL32_1: result = COFF_RelocTypeX64_REL32_1; break;
case LNK_Reloc_REL32_2: result = COFF_RelocTypeX64_REL32_2; break;
case LNK_Reloc_REL32_3: result = COFF_RelocTypeX64_REL32_3; break;
case LNK_Reloc_REL32_4: result = COFF_RelocTypeX64_REL32_4; break;
case LNK_Reloc_REL32_5: result = COFF_RelocTypeX64_REL32_5; break;
case LNK_Reloc_SECT_IDX: result = COFF_RelocTypeX64_SECTION; break;
case LNK_Reloc_SECT_REL: result = COFF_RelocTypeX64_SECREL; break;
default: InvalidPath;
}
} break;
default: lnk_not_implemented("TODO: support for machine 0x%X", machine); break;
}
return result;
}
+56
View File
@@ -0,0 +1,56 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum
{
LNK_Reloc_NULL,
LNK_Reloc_ADDR_16,
LNK_Reloc_ADDR_32,
LNK_Reloc_ADDR_64,
LNK_Reloc_CHUNK_SIZE_FILE_16,
LNK_Reloc_CHUNK_SIZE_FILE_32,
LNK_Reloc_CHUNK_SIZE_VIRT_32,
LNK_Reloc_FILE_ALIGN_32,
LNK_Reloc_FILE_OFF_15,
LNK_Reloc_FILE_OFF_32,
LNK_Reloc_FILE_OFF_64,
LNK_Reloc_REL32,
LNK_Reloc_REL32_1,
LNK_Reloc_REL32_2,
LNK_Reloc_REL32_3,
LNK_Reloc_REL32_4,
LNK_Reloc_REL32_5,
LNK_Reloc_SECT_REL,
LNK_Reloc_SECT_IDX,
LNK_Reloc_VIRT_ALIGN_32,
LNK_Reloc_VIRT_OFF_32,
} LNK_RelocType;
typedef struct LNK_Reloc
{
struct LNK_Reloc *next;
LNK_Chunk *chunk;
LNK_RelocType type;
U64 apply_off;
struct LNK_Symbol *symbol;
} LNK_Reloc;
typedef struct LNK_RelocList
{
U64 count;
LNK_Reloc *first;
LNK_Reloc *last;
} LNK_RelocList;
internal LNK_Reloc * lnk_reloc_list_reserve(Arena *arena, LNK_RelocList *list, U64 count);
internal LNK_Reloc * lnk_reloc_list_push(Arena *arena, LNK_RelocList *list);
internal LNK_RelocList lnk_reloc_list_copy(Arena *arena, LNK_RelocList *list);
internal void lnk_reloc_list_concat_in_place(LNK_RelocList *list, LNK_RelocList *to_concat);
internal void lnk_reloc_list_concat_in_place_arr(LNK_RelocList *list, LNK_RelocList *arr, U64 count);
internal LNK_RelocList ** lnk_make_reloc_list_arr_arr(Arena *arena, U64 slot_count, U64 per_count);
internal LNK_Reloc ** lnk_reloc_array_from_list(Arena *arena, LNK_RelocList list);
internal LNK_RelocType lnk_ext_reloc_type_from_coff(COFF_MachineType machine, U32 type);
internal U32 lnk_ext_reloc_type_to_coff(COFF_MachineType machine, LNK_RelocType type);
+855
View File
@@ -0,0 +1,855 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal LNK_SectionNode *
lnk_section_list_remove(LNK_SectionList *list, String8 name)
{
LNK_SectionNode *section = lnk_section_list_search_node(list, name);
if (list->count > 0) {
if (list->first == section) {
list->first = list->first->next;
list->count -= 1;
if (list->last == section) {
list->last = NULL;
}
} else {
for (LNK_SectionNode *curr = list->first, *prev = NULL; curr != NULL; prev = curr, curr = curr->next) {
if (curr == section) {
prev->next = curr->next;
list->count -= 1;
if (list->last == curr) {
list->last = prev;
}
break;
}
}
}
}
return section;
}
internal LNK_SectionNode *
lnk_section_list_search_node(LNK_SectionList *list, String8 name)
{
LNK_SectionNode *node;
for (node = list->first; node != 0; node = node->next) {
if (str8_match(node->data.name, name, 0)) {
break;
}
}
return node;
}
internal LNK_Section *
lnk_section_list_search(LNK_SectionList *list, String8 name)
{
LNK_SectionNode *node = lnk_section_list_search_node(list, name);
return node != NULL ? &node->data : NULL;
}
internal LNK_SectionArray
lnk_section_array_from_list(Arena *arena, LNK_SectionList list)
{
LNK_SectionArray result;
result.count = 0;
result.v = push_array_no_zero(arena, LNK_Section, list.count);
for (LNK_SectionNode *node = list.first; node != 0; node = node->next) {
result.v[result.count] = node->data;
result.count += 1;
}
return result;
}
internal LNK_SectionPtrArray
lnk_section_ptr_array_from_list(Arena *arena, LNK_SectionList list)
{
LNK_SectionPtrArray result;
result.count = 0;
result.v = push_array_no_zero(arena, LNK_Section *, list.count);
for (LNK_SectionNode *node = list.first; node != 0; node = node->next) {
result.v[result.count] = &node->data;
result.count += 1;
}
return result;
}
internal String8
lnk_make_section_sort_index(Arena *arena, String8 name, COFF_SectionFlags flags, U64 section_index)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
// pack sections with run-time data closer
String8List sort_index_list = {0};
if (flags & COFF_SectionFlag_MEM_DISCARDABLE) {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "a");
}
if (str8_match(name, str8_lit(".null"), 0)) {
// null section always first
str8_list_pushf(scratch.arena, &sort_index_list, "a");
} else if (str8_match(name, str8_lit(".rsrc"), 0)) {
// section with resource data must be last because during runtime windows might append pages
str8_list_pushf(scratch.arena, &sort_index_list, "c");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
}
// sort sections based on the contents
if (flags & COFF_SectionFlag_CNT_CODE) {
str8_list_pushf(scratch.arena, &sort_index_list, "a");
if (str8_match(name, str8_lit(".text"), 0)) {
str8_list_pushf(scratch.arena, &sort_index_list, "a");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
}
} else if (flags & COFF_SectionFlag_CNT_INITIALIZED_DATA) {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
if (str8_match(name, str8_lit(".data"), 0)) {
str8_list_pushf(scratch.arena, &sort_index_list, "a");
} else if (str8_match(name, str8_lit(".rdata"), 0)) {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
} else if (str8_match(name, str8_lit(".tls"), 0)) {
str8_list_pushf(scratch.arena, &sort_index_list, "c");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "d");
}
} else if (flags & COFF_SectionFlag_CNT_UNINITIALIZED_DATA) {
str8_list_pushf(scratch.arena, &sort_index_list, "c");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "d");
}
// sort sections based on read/write access so final section layout looks cleaner
if (flags & COFF_SectionFlag_MEM_READ && ~flags & COFF_SectionFlag_MEM_WRITE) {
str8_list_pushf(scratch.arena, &sort_index_list, "a");
} else {
str8_list_pushf(scratch.arena, &sort_index_list, "b");
}
String8 order_index = str8_from_bits_u32(scratch.arena, safe_cast_u32(section_index));
str8_list_push(scratch.arena, &sort_index_list, order_index);
String8 result = str8_list_join(arena, &sort_index_list, 0);
scratch_end(scratch);
ProfEnd();
return result;
}
internal void
lnk_section_associate_chunks(LNK_Section *sect, LNK_Chunk *head, LNK_Chunk *associate)
{
lnk_chunk_associate(sect->arena, head, associate);
}
internal LNK_Chunk *
lnk_section_push_chunk_raw(LNK_Section *sect, LNK_Chunk *parent, void *raw_ptr, U64 raw_size, String8 sort_index)
{
return lnk_chunk_push_leaf(sect->arena, sect->cman, parent, sort_index, raw_ptr, raw_size);
}
internal LNK_Chunk *
lnk_section_push_chunk_data(LNK_Section *sect, LNK_Chunk *parent, String8 data, String8 sort_index)
{
return lnk_section_push_chunk_raw(sect, parent, data.str, data.size, sort_index);
}
internal LNK_Chunk *
lnk_section_push_chunk_u32(LNK_Section *sect, LNK_Chunk *parent, U32 value, String8 sort_index)
{
U32 *ptr = push_array_no_zero(sect->arena, U32, 1);
*ptr = value;
return lnk_section_push_chunk_raw(sect, parent, ptr, sizeof(*ptr), sort_index);
}
internal LNK_Chunk *
lnk_section_push_chunk_u64(LNK_Section *sect, LNK_Chunk *parent, U32 value, String8 sort_index)
{
U64 *ptr = push_array_no_zero(sect->arena, U64, 1);
*ptr = value;
return lnk_section_push_chunk_raw(sect, parent, ptr, sizeof(*ptr), sort_index);
}
internal LNK_Chunk *
lnk_section_push_chunk_bss(LNK_Section *sect, LNK_Chunk *parent, U64 size, String8 sort_index)
{
return lnk_section_push_chunk_raw(sect, parent, 0, size, sort_index);
}
internal LNK_Chunk *
lnk_section_push_chunk_list(LNK_Section *sect, LNK_Chunk *parent, String8 sort_index)
{
return lnk_chunk_push_list(sect->arena, sect->cman, parent, sort_index);
}
internal LNK_Reloc *
lnk_section_push_reloc(LNK_Section *sect, LNK_Chunk *chunk, LNK_RelocType type, U64 apply_off, LNK_Symbol *symbol)
{
Assert(symbol);
LNK_Reloc *reloc = lnk_reloc_list_push(sect->arena, &sect->reloc_list);
reloc->chunk = chunk;
reloc->type = type;
reloc->apply_off = apply_off;
reloc->symbol = symbol;
return reloc;
}
internal LNK_Reloc *
lnk_section_push_reloc_undefined(LNK_Section *sect, LNK_Chunk *chunk, LNK_RelocType type, U64 apply_off, String8 undefined_symbol_name, LNK_SymbolScopeFlags scope_flags)
{
LNK_Symbol *symbol = lnk_make_undefined_symbol(sect->arena, undefined_symbol_name, scope_flags);
LNK_Reloc *reloc = lnk_section_push_reloc(sect, chunk, type, apply_off, symbol);
return reloc;
}
internal void
lnk_section_merge(LNK_Section *dst, LNK_Section *src)
{
ProfBeginFunction();
// set merge info
src->is_merged = 1;
src->merge_sect_id = dst->id;
src->id_map = push_array_no_zero(src->arena, U64, src->cman->total_chunk_count);
// put source root in a wrapper list so it has unique sort index otherwise
// after we merge sections sort indices might conflict
LNK_Chunk *src_root_wrapper = lnk_section_push_chunk_list(dst, dst->cman->root, str8(0,0));
// merge roots
lnk_merge_chunks(dst->arena, dst->cman, src_root_wrapper, src->cman->root, src->id_map, src->cman->total_chunk_count);
// copy relocations
lnk_reloc_list_concat_in_place(&dst->reloc_list, &src->reloc_list);
ProfEnd();
}
internal U8
lnk_code_align_byte_from_machine(COFF_MachineType machine)
{
U8 align_byte = 0;
switch (machine) {
case COFF_MachineType_X64:
case COFF_MachineType_X86: {
align_byte = 0xCC;
} break;
default: {
lnk_not_implemented("TODO: set align value for machine %S", coff_string_from_machine_type(machine));
} break;
}
return align_byte;
}
internal void
lnk_section_build_data(LNK_Section *sect, COFF_MachineType machine)
{
if (sect->is_loose && sect->has_layout) {
// get value for align data fill
U8 align_byte = 0;
B32 is_code = !!(sect->flags & COFF_SectionFlag_CNT_CODE);
if (is_code) {
align_byte = lnk_code_align_byte_from_machine(machine);
}
sect->layout = lnk_build_chunk_layout(sect->arena, sect->cman, sect->flags, align_byte);
sect->is_loose = 0;
}
}
internal LNK_SectionTable *
lnk_section_table_alloc(U64 section_virt_off, U64 sect_align, U64 file_align)
{
ProfBeginFunction();
Arena *arena = arena_alloc();
LNK_SectionTable *st = push_array(arena, LNK_SectionTable, 1);
st->arena = arena;
st->section_virt_off = section_virt_off;
st->sect_align = sect_align;
st->file_align = file_align;
ProfEnd();
return st;
}
internal void
lnk_section_table_release(LNK_SectionTable **st_ptr)
{
ProfBeginFunction();
LNK_SectionTable *st = *st_ptr;
arena_release(st->arena);
*st_ptr = NULL;
ProfEnd();
}
internal LNK_Section *
lnk_section_table_push(LNK_SectionTable *st, String8 name, COFF_SectionFlags flags)
{
ProfBeginFunction();
LNK_SectionList *sect_list = &st->list;
LNK_SectionNode *sect_node = push_array(st->arena, LNK_SectionNode, 1);
String8 sort_index = lnk_make_section_sort_index(st->arena, name, flags, st->id_max);
B32 found = 0;
for (LNK_SectionNode *curr = sect_list->first, *prev = NULL; curr != NULL; prev = curr, curr = curr->next) {
LNK_Section *sect = &curr->data;
int cmp = str8_compar_case_sensetive(&sort_index, &sect->sort_index);
if (cmp < 0) {
if (prev == NULL) {
SLLQueuePushFront(sect_list->first, sect_list->last, sect_node);
} else {
prev->next = sect_node;
sect_node->next = curr;
}
found = 1;
break;
}
}
if (!found) {
SLLQueuePush(sect_list->first, sect_list->last, sect_node);
}
sect_list->count += 1;
U64 sect_id = st->id_max;
st->id_max += 1;
LNK_Section *sect = &sect_node->data;
sect->arena = arena_alloc();
sect->id = sect_id;
sect->name = push_str8_copy(sect->arena, name);
sect->sort_index = sort_index;
sect->flags = flags;
sect->cman = lnk_chunk_manager_alloc(sect->arena, sect_id, st->file_align);
sect->root = sect->cman->root;
sect->nosort_chunk = lnk_chunk_push_list(sect->arena, sect->cman, sect->root, str8(0,0));
sect->nosort_chunk->sort_chunk = 0;
sect->emit_header = 1;
sect->has_layout = 1;
sect->is_loose = 1;
lnk_chunk_set_debugf(sect->arena, sect->root, "root chunk for %S", name);
ProfEnd();
return sect;
}
internal LNK_Section *
lnk_section_table_push_null(LNK_SectionTable *st)
{
LNK_SectionList *list = &st->list;
SLLQueuePushFront(list->first, list->last, st->null_sect);
list->count += 1;
return &st->null_sect->data;
}
LNK_CHUNK_VISITOR_SIG(lnk_chunk_has_leaf)
{
B32 stop = 0;
if (chunk->type == LNK_Chunk_Leaf) {
B32 has_data = !lnk_chunk_is_discarded(chunk) && chunk->u.leaf.size > 0;
if (has_data) {
B32 *no_data = (B32*)ud;
*no_data = 0;
stop = 1;
}
}
return stop;
}
LNK_CHUNK_VISITOR_SIG(lnk_chunk_mark_discarded)
{
chunk->is_discarded = 1;
B32 stop = 0;
return stop;
}
internal void
lnk_section_table_remove(LNK_SectionTable *st, LNK_SymbolTable *symtab, String8 name)
{
ProfBeginFunction();
// remove node from list
LNK_SectionNode *sect_node = lnk_section_list_remove(&st->list, name);
LNK_Section *sect = &sect_node->data;
// remove symbol for section root chunk
lnk_symbol_table_remove(symtab, LNK_SymbolScopeIndex_Internal, sect->symbol_name);
// mark chunks as discarded
lnk_visit_chunks(sect->id, sect->root, lnk_chunk_mark_discarded, NULL);
// push to empties
SLLQueuePush(st->empties_list.first, st->empties_list.last, sect_node);
st->empties_list.count += 1;
ProfEnd();
}
internal LNK_Section *
lnk_section_table_search(LNK_SectionTable *st, String8 name)
{
return lnk_section_list_search(&st->list, name);
}
internal LNK_Section *
lnk_section_table_search_id(LNK_SectionTable *st, U64 id)
{
for (LNK_SectionNode *node = st->list.first; node != NULL; node = node->next) {
if (node->data.id == id) {
return &node->data;
}
}
return NULL;
}
internal void
lnk_section_table_merge(LNK_SectionTable *st, LNK_MergeDirectiveList merge_list)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
LNK_Section **src_dst = push_array(scratch.arena, LNK_Section *, st->id_max);
for (LNK_MergeDirectiveNode *merge_node = merge_list.first; merge_node != NULL; merge_node = merge_node->next) {
LNK_MergeDirective *merge = &merge_node->data;
// are we trying to merge section that was already merged?
LNK_Section *merge_sect = lnk_section_list_search(&st->merge_list, merge->src);
if (merge_sect) {
LNK_Section *dst = src_dst[merge_sect->id];
B32 is_ambiguous_merge = !str8_match(dst->name, merge->dst, 0);
if (is_ambiguous_merge) {
lnk_error(LNK_Warning_AmbiguousMerge, "Detected ambiguous section merge:");
lnk_supplement_error("%S => %S (Merged)", merge_sect->name, dst->name);
lnk_supplement_error("%S => %S", merge_sect->name, merge->dst);
}
continue;
}
// find source seciton
LNK_Section *src = lnk_section_table_search(st, merge->src);
if (src == NULL) {
lnk_error(LNK_Warning_IllData, "Can't find section \"%S\" to merge with \"%S\"", merge->src, merge->dst);
// TODO: supplement obj path if applicable
continue;
}
// handle case where destination section doesn't exist
LNK_Section *dst = lnk_section_table_search(st, merge->dst);
if (dst == NULL) {
src->name = push_str8_copy(src->arena, merge->dst);
src_dst[src->id] = src;
continue;
}
// update map
src_dst[src->id] = dst;
// merge section with destination
lnk_section_merge(dst, src);
// remove from output section list
LNK_SectionNode *src_node = lnk_section_list_remove(&st->list, src->name);
// push section to merged list
SLLQueuePush(st->merge_list.first, st->merge_list.last, src_node);
st->merge_list.count += 1;
}
scratch_end(scratch);
ProfEnd();
}
internal void
lnk_section_table_remove_empties(LNK_SectionTable *st, LNK_SymbolTable *symtab)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
String8List name_list = {0};
for (LNK_SectionNode *sect_node = st->list.first; sect_node != NULL; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
B32 no_data = 1;
lnk_visit_chunks(sect->id, sect->root, lnk_chunk_has_leaf, (void*)&no_data);
if (no_data) {
String8 name = push_str8_copy(scratch.arena, sect->name);
str8_list_push(scratch.arena, &name_list, name);
}
}
for (String8Node *name = name_list.first; name != NULL; name = name->next) {
lnk_section_table_remove(st, symtab, name->string);
}
scratch_end(scratch);
ProfEnd();
}
internal LNK_SectionArray
lnk_section_table_get_output_sections(Arena *arena, LNK_SectionTable *st)
{
LNK_SectionArray result = {0};
result.count = 0;
result.v = push_array(arena, LNK_Section, st->list.count);
for (LNK_SectionNode *sect_node = st->list.first; sect_node != 0; sect_node = sect_node->next) {
if (sect_node->data.emit_header && sect_node->data.has_layout) {
Assert(result.count < st->list.count);
result.v[result.count] = sect_node->data;
result.count += 1;
}
}
U64 unused_entry_count = st->list.count - result.count;
arena_pop(arena, unused_entry_count * sizeof(result.v[0]));
return result;
}
internal
THREAD_POOL_TASK_FUNC(lnk_section_data_builder)
{
LNK_SectionDataBuilder *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
for (U64 sect_idx = range.min; sect_idx < range.max; ++sect_idx) {
lnk_section_build_data(task->sect_arr[sect_idx], task->machine);
}
}
internal void
lnk_section_table_build_data(TP_Context *tp, LNK_SectionTable *st, COFF_MachineType machine)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
LNK_SectionPtrArray sect_arr = lnk_section_ptr_array_from_list(scratch.arena, st->list);
LNK_SectionDataBuilder task = {0};
task.machine = machine;
task.range_arr = tp_divide_work(scratch.arena, sect_arr.count, tp->worker_count);
task.sect_arr = sect_arr.v;
tp_for_parallel(tp, 0, tp->worker_count, lnk_section_data_builder, &task);
scratch_end(scratch);
ProfEnd();
}
internal void
lnk_section_table_assign_virtual_offsets(LNK_SectionTable *st)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
LNK_Section **sect_id_map = lnk_sect_id_map_from_section_table(scratch.arena, st);
U64 cursor = st->section_virt_off;
Assert(cursor >= 0x1000);
for (LNK_SectionNode *sect_node = st->list.first; sect_node != NULL; sect_node = sect_node->next) {
if (sect_node == st->null_sect) continue;
LNK_Section *sect = &sect_node->data;
if (!sect->has_layout) continue;
sect->virt_off = cursor;
U64 sect_size = lnk_virt_size_from_chunk_ref(sect_id_map, sect->root->ref);
cursor += sect_size;
cursor = AlignPow2(cursor, st->sect_align);
}
scratch_end(scratch);
ProfEnd();
}
internal void
lnk_section_table_assign_file_offsets(LNK_SectionTable *st)
{
ProfBeginFunction();
U64 cursor = 0;
for (LNK_SectionNode *sect_node = st->list.first; sect_node != NULL; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
if (sect->flags & COFF_SectionFlag_CNT_UNINITIALIZED_DATA) {
continue;
}
if (!sect->has_layout) continue;
sect->file_off = cursor;
U64 root_size = sect->layout.chunk_file_size_array[sect->root->ref.chunk_id];
cursor += root_size;
}
ProfEnd();
}
internal void
lnk_section_table_assign_indices(LNK_SectionTable *st)
{
ProfBeginFunction();
U64 isect = 0;
for (LNK_SectionNode *sect_node = st->list.first; sect_node != NULL; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
if (sect->emit_header) {
sect->isect = isect++;
}
}
ProfEnd();
}
internal String8
lnk_section_table_serialize(Arena *arena, LNK_SectionTable *st)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
String8List image_list = {0};
for (LNK_SectionNode *sect_node = st->list.first; sect_node != NULL; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
str8_list_push(scratch.arena, &image_list, sect->layout.data);
}
String8 result = str8_list_join(arena, &image_list, NULL);
scratch_end(scratch);
ProfEnd();
return result;
}
internal LNK_ChunkPtr **
lnk_chunk_id_map_from_section_table(Arena *arena, LNK_SectionTable *st)
{
ProfBeginFunction();
LNK_ChunkPtr **chunk_id_map = push_array(arena, LNK_ChunkPtr *, st->id_max);
for (LNK_SectionNode *node = st->list.first; node != 0; node = node->next) {
LNK_Section *sect = &node->data;
chunk_id_map[sect->id] = lnk_make_chunk_id_map(arena, sect->cman);
}
if (st->list.first->data.id != 0) {
chunk_id_map[0] = push_array(arena, LNK_ChunkPtr, 1);
chunk_id_map[0][0] = g_null_chunk_ptr;
}
ProfEnd();
return chunk_id_map;
}
internal LNK_Section **
lnk_sect_id_map_from_section_table(Arena *arena, LNK_SectionTable *st)
{
ProfBeginFunction();
LNK_Section **map = push_array(arena, LNK_Section *, st->id_max);
LNK_SectionList *list_arr[] = { &st->list, &st->merge_list, &st->empties_list };
for (U64 list_idx = 0; list_idx < ArrayCount(list_arr); ++list_idx) {
for (LNK_SectionNode *sect_node = list_arr[list_idx]->first; sect_node != NULL; sect_node = sect_node->next) {
LNK_Section *sect = &sect_node->data;
Assert(sect->id < st->id_max);
Assert(map[sect->id] == NULL);
map[sect->id] = sect;
}
}
if (map[0] == NULL) {
LNK_Section *sect = push_array(arena, LNK_Section, 1);
sect->layout.chunk_off_array = push_array(arena, U64, 1);
sect->layout.chunk_file_size_array = push_array(arena, U64, 1);
sect->layout.chunk_virt_size_array = push_array(arena, U64, 1);
map[0] = sect;
}
ProfEnd();
return map;
}
internal LNK_ChunkRef
lnk_get_final_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = chunk_ref;
if (sect_id_map[chunk_ref.sect_id]->is_merged) {
final_chunk_ref.sect_id = sect_id_map[chunk_ref.sect_id]->merge_sect_id;
final_chunk_ref.chunk_id = sect_id_map[chunk_ref.sect_id]->id_map[chunk_ref.chunk_id];
// we don't support sections that were merged more than once.
Assert(!sect_id_map[final_chunk_ref.sect_id]->is_merged);
}
return final_chunk_ref;
}
internal LNK_Section *
lnk_sect_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef input_chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, input_chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
return sect;
}
internal LNK_Chunk *
lnk_chunk_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkPtr **chunk_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Chunk *chunk = chunk_id_map[final_chunk_ref.sect_id][final_chunk_ref.chunk_id];
return chunk;
}
internal U64
lnk_isect_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_Section *sect = lnk_sect_from_chunk_ref(sect_id_map, chunk_ref);
U64 isect = sect->isect;
return isect;
}
internal U64
lnk_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 off = sect->layout.chunk_off_array[final_chunk_ref.chunk_id];
return off;
}
internal U64
lnk_virt_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 off = sect->layout.chunk_off_array[final_chunk_ref.chunk_id];
U64 virt_off = off + sect->virt_off;
return virt_off;
}
internal U64
lnk_file_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 off = sect->layout.chunk_off_array[final_chunk_ref.chunk_id];
U64 file_off = off + sect->file_off;
return file_off;
}
internal U64
lnk_virt_size_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 virt_size = sect->layout.chunk_virt_size_array[final_chunk_ref.chunk_id];
return virt_size;
}
internal U64
lnk_file_size_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 file_size = sect->layout.chunk_file_size_array[final_chunk_ref.chunk_id];
return file_size;
}
internal String8
lnk_data_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 chunk_off = lnk_off_from_chunk_ref(sect_id_map, chunk_ref);
U64 chunk_size = lnk_file_size_from_chunk_ref(sect_id_map, chunk_ref);
String8 chunk_data = str8_substr(sect->layout.data, r1u64(chunk_off, chunk_off + chunk_size));
return chunk_data;
}
internal String8
lnk_data_from_chunk_ref_no_pad(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
LNK_ChunkRef final_chunk_ref = lnk_get_final_chunk_ref(sect_id_map, chunk_ref);
LNK_Section *sect = sect_id_map[final_chunk_ref.sect_id];
U64 chunk_off = lnk_off_from_chunk_ref(sect_id_map, chunk_ref);
U64 chunk_size = lnk_virt_size_from_chunk_ref(sect_id_map, chunk_ref);
String8 chunk_data = str8_substr(sect->layout.data, r1u64(chunk_off, chunk_off + chunk_size));
return chunk_data;
}
internal ISectOff
lnk_sc_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref)
{
ISectOff sc = {0};
sc.isect = lnk_isect_from_chunk_ref(sect_id_map, chunk_ref);
sc.off = lnk_off_from_chunk_ref(sect_id_map, chunk_ref);
return sc;
}
internal U64
lnk_virt_off_from_reloc(LNK_Section **sect_id_map, LNK_Reloc *reloc)
{
U64 virt_off = lnk_virt_off_from_chunk_ref(sect_id_map, reloc->chunk->ref);
virt_off += reloc->apply_off;
return virt_off;
}
internal U64
lnk_isect_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
LNK_ChunkRef symbol_chunk_ref = symbol->u.defined.u.chunk->ref;
U64 symbol_isect = lnk_isect_from_chunk_ref(sect_id_map, symbol_chunk_ref);
return symbol_isect;
}
internal U64
lnk_sect_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
LNK_ChunkRef symbol_chunk_ref = symbol->u.defined.u.chunk->ref;
U64 chunk_off = lnk_off_from_chunk_ref(sect_id_map, symbol_chunk_ref);
U64 symbol_off = chunk_off + symbol->u.defined.u.chunk_offset;
return symbol_off;
}
internal U64
lnk_virt_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
LNK_ChunkRef symbol_chunk_ref = symbol->u.defined.u.chunk->ref;
U64 chunk_voff = lnk_virt_off_from_chunk_ref(sect_id_map, symbol_chunk_ref);
U64 symbol_voff = chunk_voff + symbol->u.defined.u.chunk_offset;
return symbol_voff;
}
internal U64
lnk_file_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
LNK_ChunkRef symbol_chunk_ref = symbol->u.defined.u.chunk->ref;
U64 chunk_foff = lnk_file_off_from_chunk_ref(sect_id_map, symbol_chunk_ref);
U64 symbol_foff = chunk_foff + symbol->u.defined.u.chunk_offset;
return symbol_foff;
}
internal U64
lnk_virt_size_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
U64 symbol_chunk_virt_size = lnk_virt_size_from_chunk_ref(sect_id_map, symbol->u.defined.u.chunk->ref);
return symbol_chunk_virt_size;
}
internal U64
lnk_file_size_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol)
{
Assert(LNK_Symbol_IsDefined(symbol->type));
U64 symbol_chunk_file_size = lnk_file_size_from_chunk_ref(sect_id_map, symbol->u.defined.u.chunk->ref);
return symbol_chunk_file_size;
}
#if LNK_DEBUG_CHUNKS
internal void
lnk_dump_chunks(LNK_SectionTable *st)
{
Temp scratch = scratch_begin(0, 0);
LNK_ChunkPtr **chunk_id_map = lnk_chunk_id_map_from_section_table(scratch.arena, st);
LNK_Section **sect_id_map = lnk_sect_id_map_from_section_table(scratch.arena, st);
for (U64 sect_id = 0; sect_id < st->id_max; ++sect_id) {
LNK_Section *sect = sect_id_map[sect_id];
if (!sect) continue;
if (sect->is_merged) continue;
if (str8_match(sect->name, str8_lit(".text"), 0)) {
for (U64 chunk_id = 0; chunk_id < sect->cman->total_chunk_count; ++chunk_id) {
LNK_ChunkRef chunk_ref = { sect_id, chunk_id };
LNK_Chunk *chunk = lnk_chunk_from_chunk_ref(sect_id_map, chunk_id_map, chunk_ref);
U64 chunk_foff = sect->file_off + sect->layout.chunk_off_array[chunk_id];
printf("%llu {%04llX,%04llX} 0x%08llX %.*s\n", chunk_foff, sect_id, chunk_id, chunk_foff, str8_varg(chunk->debug));
}
}
}
scratch_end(scratch);
}
#endif
+151
View File
@@ -0,0 +1,151 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef struct LNK_Section
{
Arena *arena;
U64 id;
String8 name;
String8 symbol_name;
COFF_SectionFlags flags;
String8 sort_index;
LNK_ChunkManager *cman;
LNK_Chunk *root;
// overwhelming number of chunks don't have sort index and grouping
// them speeds up sort step
LNK_Chunk *nosort_chunk;
LNK_RelocList reloc_list;
B32 emit_header; // TODO: this is a hack to make reloc serializer work in resource converter
B32 has_layout;
B32 is_loose;
B32 is_merged;
U64 merge_sect_id;
U64 *id_map;
U64 isect;
U64 virt_off;
U64 file_off;
LNK_ChunkLayout layout;
} LNK_Section;
typedef struct LNK_SectionNode
{
struct LNK_SectionNode *next;
LNK_Section data;
} LNK_SectionNode;
typedef struct LNK_SectionList
{
U64 count;
LNK_SectionNode *first;
LNK_SectionNode *last;
} LNK_SectionList;
typedef struct LNK_SectionArray
{
U64 count;
LNK_Section *v;
} LNK_SectionArray;
typedef struct LNK_SectionPtrArray
{
U64 count;
LNK_Section **v;
} LNK_SectionPtrArray;
typedef struct LNK_SectionTable
{
Arena *arena;
U64 section_virt_off;
U64 sect_align;
U64 file_align;
U64 id_max;
LNK_SectionList list;
LNK_SectionList merge_list;
LNK_SectionList empties_list;
LNK_SectionNode *null_sect;
} LNK_SectionTable;
////////////////////////////////
typedef struct
{
COFF_MachineType machine;
Rng1U64 *range_arr;
LNK_Section **sect_arr;
} LNK_SectionDataBuilder;
////////////////////////////////
internal LNK_SectionNode * lnk_section_list_remove(LNK_SectionList *list, String8 name);
internal LNK_SectionNode * lnk_section_list_search_node(LNK_SectionList *list, String8 name);
internal LNK_Section * lnk_section_list_search(LNK_SectionList *list, String8 name);
internal LNK_SectionArray lnk_section_array_from_list(Arena *arena, LNK_SectionList list);
internal LNK_SectionPtrArray lnk_section_ptr_array_from_list(Arena *arena, LNK_SectionList list);
internal void lnk_section_associate_chunks(LNK_Section *sect, LNK_Chunk *head, LNK_Chunk *associate);
internal LNK_Reloc * lnk_section_push_reloc(LNK_Section *sect, LNK_Chunk *chunk, LNK_RelocType type, U64 apply_off, LNK_Symbol *symbol);
internal LNK_Reloc * lnk_section_push_reloc_undefined(LNK_Section *sect, LNK_Chunk *chunk, LNK_RelocType type, U64 apply_off, String8 undefined_symbol_name, LNK_SymbolScopeFlags scope_flags);
internal void lnk_section_merge(LNK_Section *dst, LNK_Section *src);
internal void lnk_section_build_data(LNK_Section *sect, COFF_MachineType machine);
internal String8 lnk_make_section_sort_index(Arena *arena, String8 name, COFF_SectionFlags flags, U64 section_index);
internal LNK_Chunk * lnk_section_push_chunk_raw(LNK_Section *sect, LNK_Chunk *parent, void *data_ptr, U64 data_size, String8 sort_index);
internal LNK_Chunk * lnk_section_push_chunk_data(LNK_Section *sect, LNK_Chunk *parent, String8 data, String8 sort_index);
internal LNK_Chunk * lnk_section_push_chunk_u32(LNK_Section *sect, LNK_Chunk *parent, U32 value, String8 sort_index);
internal LNK_Chunk * lnk_section_push_chunk_u64(LNK_Section *sect, LNK_Chunk *parent, U32 value, String8 sort_index);
internal LNK_Chunk * lnk_section_push_chunk_bss(LNK_Section *sect, LNK_Chunk *parent, U64 size, String8 sort_index);
internal LNK_Chunk * lnk_section_push_chunk_list(LNK_Section *sect, LNK_Chunk *parent, String8 sort_index);
internal LNK_SectionTable * lnk_section_table_alloc(U64 section_virt_off, U64 sect_align, U64 file_align);
internal void lnk_section_table_release(LNK_SectionTable **st_ptr);
internal LNK_Section * lnk_section_table_push(LNK_SectionTable *st, String8 name, COFF_SectionFlags flags);
internal LNK_Section * lnk_section_table_push_null(LNK_SectionTable *st);
internal void lnk_section_table_remove(LNK_SectionTable *st, LNK_SymbolTable *symtab, String8 name);
internal LNK_Section * lnk_section_table_search(LNK_SectionTable *st, String8 name);
internal LNK_Section * lnk_section_table_search_id(LNK_SectionTable *st, U64 id);
internal void lnk_section_table_merge(LNK_SectionTable *st, LNK_MergeDirectiveList merge_list);
internal void lnk_section_table_remove_empties(LNK_SectionTable *st, LNK_SymbolTable *symtab);
internal void lnk_section_table_build_data(TP_Context *tp, LNK_SectionTable *st, COFF_MachineType machine);
internal void lnk_section_table_assign_virtual_offsets(LNK_SectionTable *st);
internal void lnk_section_table_assign_file_offsets(LNK_SectionTable *st);
internal void lnk_section_table_assign_indices(LNK_SectionTable *st);
internal String8 lnk_section_table_serialize(Arena *arena, LNK_SectionTable *st);
internal LNK_ChunkPtr ** lnk_chunk_id_map_from_section_table(Arena *arena, LNK_SectionTable *st);
internal LNK_Section ** lnk_sect_id_map_from_section_table(Arena *arena, LNK_SectionTable *st);
internal LNK_ChunkRef lnk_get_final_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal LNK_Section * lnk_sect_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal LNK_Chunk * lnk_chunk_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkPtr **chunk_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_isect_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_virt_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_file_off_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_virt_size_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_file_size_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal String8 lnk_data_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal String8 lnk_data_from_chunk_ref_no_pad(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal ISectOff lnk_sc_from_chunk_ref(LNK_Section **sect_id_map, LNK_ChunkRef chunk_ref);
internal U64 lnk_virt_off_from_reloc(LNK_Section **sect_id_map, LNK_Reloc *reloc);
internal U64 lnk_isect_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
internal U64 lnk_sect_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
internal U64 lnk_virt_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
internal U64 lnk_file_off_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
internal U64 lnk_virt_size_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
internal U64 lnk_file_size_from_symbol(LNK_Section **sect_id_map, LNK_Symbol *symbol);
#if LNK_DEBUG_CHUNKS
internal void lnk_dump_chunks(LNK_SectionTable *st);
#endif
+832
View File
@@ -0,0 +1,832 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
global read_only LNK_Symbol g_null_symbol = { str8_lit_comp("NULL"), LNK_Symbol_DefinedStatic };
global read_only LNK_Symbol *g_null_symbol_ptr = &g_null_symbol;
internal void
lnk_init_symbol(LNK_Symbol *symbol, String8 name, LNK_SymbolType type)
{
symbol->name = name;
symbol->type = type;
}
internal void
lnk_init_defined_symbol(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags)
{
switch (visibility) {
case LNK_DefinedSymbolVisibility_Static: lnk_init_symbol(symbol, name, LNK_Symbol_DefinedStatic); break;
case LNK_DefinedSymbolVisibility_Extern: lnk_init_symbol(symbol, name, LNK_Symbol_DefinedExtern); break;
case LNK_DefinedSymbolVisibility_Internal: lnk_init_symbol(symbol, name, LNK_Symbol_DefinedInternal); break;
}
LNK_DefinedSymbol *def = &symbol->u.defined;
def->flags = flags;
def->value_type = LNK_DefinedSymbolValue_Null;
}
internal void
lnk_init_defined_symbol_chunk(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, LNK_Chunk *chunk, U64 offset, COFF_ComdatSelectType selection, U32 check_sum)
{
lnk_init_defined_symbol(symbol, name, visibility, flags);
LNK_DefinedSymbol *def = &symbol->u.defined;
def->value_type = LNK_DefinedSymbolValue_Chunk;
def->u.chunk = chunk;
def->u.chunk_offset = offset;
def->u.check_sum = check_sum;
def->u.selection = selection;
}
internal void
lnk_init_defined_symbol_va(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, U64 va)
{
lnk_init_defined_symbol(symbol, name, visibility, flags);
LNK_DefinedSymbol *def = &symbol->u.defined;
def->value_type = LNK_DefinedSymbolValue_VA;
def->u.va = va;
}
internal void
lnk_init_undefined_symbol(LNK_Symbol *symbol, String8 name, LNK_SymbolScopeFlags scope_flags)
{
lnk_init_symbol(symbol, name, LNK_Symbol_Undefined);
symbol->u.undefined.scope_flags = scope_flags;
}
internal void
lnk_init_weak_symbol(LNK_Symbol *symbol, String8 name, COFF_WeakExtType lookup, LNK_Symbol *fallback)
{
lnk_init_symbol(symbol, name, LNK_Symbol_Weak);
symbol->u.weak.scope_flags = LNK_SymbolScopeFlag_Defined;
symbol->u.weak.lookup_type = lookup;
symbol->u.weak.fallback_symbol = fallback;
}
internal void
lnk_init_lazy_symbol(LNK_Symbol *symbol, String8 name, LNK_Lib *lib, U64 member_offset)
{
lnk_init_symbol(symbol, name, LNK_Symbol_Lazy);
symbol->u.lazy.lib = lib;
symbol->u.lazy.member_offset = member_offset;
}
internal LNK_Symbol *
lnk_make_defined_symbol(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_defined_symbol(symbol, name, visibility, flags);
return symbol;
}
internal LNK_Symbol *
lnk_make_defined_symbol_chunk(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, LNK_Chunk *chunk, U64 offset, COFF_ComdatSelectType selection, U32 check_sum)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_defined_symbol_chunk(symbol, name, visibility, flags, chunk, offset, selection, check_sum);
return symbol;
}
internal LNK_Symbol *
lnk_make_defined_symbol_va(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, U64 va)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_defined_symbol_va(symbol, name, visibility, flags, va);
return symbol;
}
internal LNK_Symbol *
lnk_make_undefined_symbol(Arena *arena, String8 name, LNK_SymbolScopeFlags flags)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_undefined_symbol(symbol, name, flags);
return symbol;
}
internal LNK_Symbol *
lnk_make_weak_symbol(Arena *arena, String8 name, COFF_WeakExtType lookup, LNK_Symbol *fallback)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_weak_symbol(symbol, name, lookup, fallback);
return symbol;
}
internal LNK_Symbol *
lnk_make_lazy_symbol(Arena *arena, String8 name, LNK_Lib *lib, U64 member_offset)
{
LNK_Symbol *symbol = push_array_no_zero(arena, LNK_Symbol, 1);
lnk_init_lazy_symbol(symbol, name, lib, member_offset);
return symbol;
}
internal LNK_Chunk *
lnk_defined_symbol_get_chunk(LNK_DefinedSymbol *symbol)
{
if (symbol->value_type == LNK_DefinedSymbolValue_Chunk) {
return symbol->u.chunk;
}
return 0;
}
internal void
lnk_symbol_list_push_node(LNK_SymbolList *list, LNK_SymbolNode *node)
{
DLLPushBack(list->first, list->last, node);
list->count += 1;
}
internal LNK_SymbolNode *
lnk_symbol_list_push(Arena *arena, LNK_SymbolList *list, LNK_Symbol *symbol)
{
LNK_SymbolNode *node = push_array(arena, LNK_SymbolNode, 1);
node->data = symbol;
lnk_symbol_list_push_node(list, node);
return node;
}
internal void
lnk_symbol_list_push_list(LNK_SymbolList *list, LNK_SymbolList *to_push)
{
if (to_push->count) {
if (list->count) {
list->last->next = to_push->first;
to_push->first->prev = list->last;
list->last = to_push->last;
list->count += to_push->count;
} else {
*list = *to_push;
}
MemoryZeroStruct(to_push);
}
}
internal void
lnk_symbol_list_insert_after(LNK_SymbolList *list, LNK_SymbolNode *node, LNK_SymbolNode *insert)
{
DLLInsert(list->first, list->last, node, insert);
list->count += 1;
}
internal LNK_SymbolNode *
lnk_symbol_list_pop_node(LNK_SymbolList *list)
{
LNK_SymbolNode *node = 0;
if (list->count) {
node = list->first;
DLLRemove(list->first, list->last, node);
node->next = 0;
node->prev = 0;
list->count -= 1;
}
return node;
}
internal LNK_Symbol *
lnk_symbol_list_pop(LNK_SymbolList *list)
{
LNK_SymbolNode *node = lnk_symbol_list_pop_node(list);
return node ? node->data : 0;
}
internal void
lnk_symbol_list_remove(LNK_SymbolList *list, LNK_SymbolNode *node)
{
Assert(list->count > 0);
list->count -= 1;
DLLRemove(list->first, list->last, node);
node->next = 0;
node->prev = 0;
}
internal void
lnk_symbol_list_concat_in_place(LNK_SymbolList *list, LNK_SymbolList *to_concat)
{
DLLConcatInPlace(list, to_concat);
}
internal LNK_SymbolList
lnk_symbol_list_copy(Arena *arena, LNK_SymbolList list)
{
LNK_SymbolList result = {0};
LNK_SymbolNode *node_arr = push_array_no_zero(arena, LNK_SymbolNode, list.count);
for (LNK_SymbolNode *i = list.first; i != 0; i = i->next) {
Assert(result.count < list.count);
LNK_SymbolNode *n = &node_arr[result.count++];
n->data = i->data;
SLLQueuePush(result.first, result.last, n);
}
return result;
}
internal LNK_SymbolNode *
lnk_symbol_list_search_node(LNK_SymbolList list, String8 name, StringMatchFlags flags)
{
for (LNK_SymbolNode *node = list.first; node != 0; node = node->next) {
if (str8_match(node->data->name, name, flags)) {
return node;
}
}
return 0;
}
internal LNK_Symbol *
lnk_symbol_list_search(LNK_SymbolList list, String8 name, StringMatchFlags flags)
{
LNK_SymbolNode *node = lnk_symbol_list_search_node(list, name, flags);
return node ? node->data : 0;
}
internal LNK_SymbolList
lnk_symbol_list_from_array(Arena *arena, LNK_SymbolArray arr)
{
LNK_SymbolList list = {0};
LNK_SymbolNode *node_arr = push_array_no_zero(arena, LNK_SymbolNode, arr.count);
for (U64 i = 0; i < arr.count; i += 1) {
LNK_SymbolNode *node = &node_arr[i];
node->prev = node->next = 0;
node->data = &arr.v[i];
lnk_symbol_list_push_node(&list, node);
}
return list;
}
internal LNK_SymbolNodeArray
lnk_symbol_node_array_from_list(Arena *arena, LNK_SymbolList list)
{
LNK_SymbolNodeArray result = {0};
result.count = 0;
result.v = push_array_no_zero(arena, LNK_SymbolNode *, list.count);
for (LNK_SymbolNode *i = list.first; i != 0; i = i->next, ++result.count) {
result.v[result.count] = i;
}
return result;
}
internal
THREAD_POOL_TASK_FUNC(lnk_symbol_node_ptr_hasher)
{
LNK_SymbolNodePtrHasher *hasher = raw_task;
Rng1U64 range = hasher->range_arr[task_id];
for (U64 symbol_idx = range.min; symbol_idx < range.max; symbol_idx += 1) {
LNK_SymbolNode *symbol_node = hasher->input_arr[symbol_idx];
symbol_node->hash = lnk_symbol_table_hash(symbol_node->data->name);
}
}
internal void
lnk_symbol_node_ptr_array_hash(TP_Context *tp, LNK_SymbolNode **arr, U64 count)
{
Temp scratch = scratch_begin(0, 0);
LNK_SymbolNodePtrHasher hasher = {0};
hasher.input_arr = arr;
hasher.range_arr = tp_divide_work(scratch.arena, count, tp->worker_count);
tp_for_parallel(tp, 0, tp->worker_count, lnk_symbol_node_ptr_hasher, &hasher);
scratch_end(scratch);
}
internal
THREAD_POOL_TASK_FUNC(lnk_symbol_node_hasher)
{
LNK_SymbolNodeHasher *hasher = raw_task;
Rng1U64 range = hasher->range_arr[task_id];
for (U64 symbol_idx = range.min; symbol_idx < range.max; symbol_idx += 1) {
LNK_SymbolNode *symbol_node = &hasher->input_arr[symbol_idx];
symbol_node->hash = lnk_symbol_table_hash(symbol_node->data->name);
}
}
internal void
lnk_symbol_node_array_hash(TP_Context *tp, LNK_SymbolNode *arr, U64 count)
{
Temp scratch = scratch_begin(0, 0);
LNK_SymbolNodeHasher hasher = {0};
hasher.input_arr = arr;
hasher.range_arr = tp_divide_work(scratch.arena, count, tp->worker_count);
tp_for_parallel(tp, 0, tp->worker_count, lnk_symbol_node_hasher, &hasher);
scratch_end(scratch);
}
internal LNK_SymbolArray
lnk_symbol_array_from_list(Arena *arena, LNK_SymbolList list)
{
LNK_SymbolArray arr = {0};
arr.count = 0;
arr.v = push_array_no_zero(arena, LNK_Symbol, list.count);
for (LNK_SymbolNode *node = list.first; node != 0; node = node->next) {
arr.v[arr.count++] = *node->data;
}
return arr;
}
internal LNK_Symbol *
lnk_symbol_array_search(LNK_SymbolArray symarr, String8 name, StringMatchFlags flags)
{
for (U64 isym = 0; isym < symarr.count; ++isym) {
LNK_Symbol *sym = &symarr.v[isym];
if (str8_match(sym->name, name, flags)) {
return sym;
}
}
return 0;
}
internal
THREAD_POOL_TASK_FUNC(lnk_symbol_name_hasher)
{
LNK_SymbolNameHasher *task = raw_task;
Rng1U64 range = task->range_arr[task_id];
for (U64 symbol_idx = range.min; symbol_idx < range.max; symbol_idx += 1) {
LNK_Symbol *symbol = &task->symbol_arr[symbol_idx];
task->hash_arr[symbol_idx] = lnk_symbol_table_hash(symbol->name);
}
}
internal U64 *
lnk_symbol_array_hash(TP_Context *tp, Arena *arena, LNK_Symbol *arr, U64 count)
{
Temp scratch = scratch_begin(&arena, 1);
U64 stride = CeilIntegerDiv(count, tp->worker_count);
Rng1U64 *range_arr = push_array_no_zero(scratch.arena, Rng1U64, tp->worker_count);
for (U64 thread_idx = 0; thread_idx < tp->worker_count; thread_idx += 1) {
Rng1U64 *range = &range_arr[thread_idx];
range->min = Min(count, stride * thread_idx);
range->max = Min(count, range->min + stride);
}
LNK_SymbolNameHasher hasher_ctx = {0};
hasher_ctx.symbol_arr = arr;
hasher_ctx.range_arr = range_arr;
hasher_ctx.hash_arr = push_array_no_zero(arena, U64, count);
tp_for_parallel(tp, 0, tp->worker_count, lnk_symbol_name_hasher, &hasher_ctx);
scratch_end(scratch);
return hasher_ctx.hash_arr;
}
internal LNK_SymbolTable *
lnk_symbol_table_alloc(void)
{
return lnk_symbol_table_alloc_ex(0x1000, 0x100, 0x500, 0x1000);
}
internal LNK_SymbolTable *
lnk_symbol_table_alloc_ex(U64 defined_cap, U64 internal_cap, U64 weak_cap, U64 lib_cap)
{
ProfBeginDynamic("Alloc Symbol Table [Defined: 0x%llx, Internal: 0x%llx, Weak: 0x%llx, Lib: 0x%llx]", defined_cap, internal_cap, weak_cap, lib_cap);
Arena *arena = arena_alloc();
LNK_SymbolTable *symtab = push_array(arena, LNK_SymbolTable, 1);
symtab->arena = arena;
symtab->bucket_count[LNK_SymbolScopeIndex_Defined] = defined_cap;
symtab->bucket_count[LNK_SymbolScopeIndex_Internal] = internal_cap;
symtab->bucket_count[LNK_SymbolScopeIndex_Weak] = weak_cap;
symtab->bucket_count[LNK_SymbolScopeIndex_Lib] = lib_cap;
for (U64 iscope = 0; iscope < ArrayCount(symtab->buckets); ++iscope) {
symtab->buckets[iscope] = push_array(symtab->arena, LNK_SymbolList, symtab->bucket_count[iscope]);
}
ProfEnd();
return symtab;
}
internal void
lnk_symbol_table_release(LNK_SymbolTable **symtab)
{
ProfBeginFunction();
arena_release((*symtab)->arena);
*symtab = 0;
ProfEnd();
}
internal U64
lnk_symbol_table_hash(String8 string)
{
return hash_from_str8(string);
}
internal LNK_SymbolNode *
lnk_symbol_table_search_bucket(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope_idx, U64 bucket_idx, String8 name, U64 hash)
{
for (LNK_SymbolNode *node = symtab->buckets[scope_idx][bucket_idx].first; node != 0; node = node->next) {
if (hash == node->hash && str8_match(node->data->name, name, 0)) {
return node;
}
}
return 0;
}
internal LNK_SymbolNode *
lnk_symbol_table_search_node_hash(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, String8 name, U64 hash)
{
while (scope_flags) {
LNK_SymbolScopeIndex scope_idx = ctz64(scope_flags);
scope_flags &= scope_flags - 1;
U64 bucket_idx = hash % symtab->bucket_count[scope_idx];
LNK_SymbolNode *node = lnk_symbol_table_search_bucket(symtab, scope_idx, bucket_idx, name, hash);
if (node) return node;
}
return 0;
}
internal LNK_SymbolNode *
lnk_symbol_table_search_node(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, String8 name)
{
U64 hash = lnk_symbol_table_hash(name);
return lnk_symbol_table_search_node_hash(symtab, scope_flags, name, hash);
}
internal LNK_Symbol *
lnk_symbol_table_search(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, String8 name)
{
LNK_SymbolNode *node = lnk_symbol_table_search_node(symtab, scope_flags, name);
return node ? node->data : 0;
}
internal LNK_Symbol *
lnk_symbol_table_searchf(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, char *fmt, ...)
{
Temp scratch = scratch_begin(0, 0);
va_list args;
va_start(args, fmt);
String8 name = push_str8fv(scratch.arena, fmt, args);
va_end(args);
LNK_Symbol *symbol = lnk_symbol_table_search(symtab, scope_flags, name);
scratch_end(scratch);
return symbol;
}
internal void
lnk_symbol_table_remove(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope, String8 name)
{
U64 hash = lnk_symbol_table_hash(name);
U64 ibucket = hash % symtab->bucket_count[scope];
for (;;) {
LNK_SymbolNode *node = lnk_symbol_table_search_bucket(symtab, scope, ibucket, name, hash);
if (!node) {
break;
}
LNK_SymbolList *bucket = &symtab->buckets[scope][ibucket];
DLLRemove(bucket->first, bucket->last, node);
bucket->count -= 1;
}
}
internal LNK_SymbolList *
lnk_symbol_table_bucket_from_hash(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope_idx, U64 hash)
{
U64 bucket_idx = hash % symtab->bucket_count[scope_idx];
LNK_SymbolList *bucket = &symtab->buckets[scope_idx][bucket_idx];
return bucket;
}
internal void
lnk_symbol_table_push_(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope_idx, LNK_SymbolNode *node, U64 hash)
{
LNK_SymbolList *bucket = lnk_symbol_table_bucket_from_hash(symtab, scope_idx, hash);
node->hash = hash;
lnk_symbol_list_push_node(bucket, node);
}
internal void
lnk_symbol_table_push_node_hash(LNK_SymbolTable *symtab, LNK_SymbolNode *node, U64 hash)
{
switch (node->data->type) {
case LNK_Symbol_Null: break;
case LNK_Symbol_DefinedExtern: {
lnk_symbol_table_push_(symtab, LNK_SymbolScopeIndex_Defined, node, hash);
} break;
case LNK_Symbol_DefinedInternal: {
lnk_symbol_table_push_(symtab, LNK_SymbolScopeIndex_Internal, node, hash);
} break;
case LNK_Symbol_Weak: {
LNK_SymbolNode *is_strong_defn_present = lnk_symbol_table_search_node(symtab, LNK_SymbolScopeFlag_Defined, node->data->name);
if (is_strong_defn_present) {
break;
}
LNK_SymbolNode *is_weak_present = lnk_symbol_table_search_node(symtab, LNK_SymbolScopeFlag_Weak, node->data->name);
if (is_weak_present) {
B32 is_fallback_same = str8_match(is_weak_present->data->u.weak.fallback_symbol->name, node->data->u.weak.fallback_symbol->name, 0);
if (!is_fallback_same) {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "Weak symbol %S conflict detected, symbol defined in:", node->data->name);
lnk_supplement_error("%S", node->data->debug);
lnk_supplement_error("%S", is_weak_present->data->debug);
}
break;
}
lnk_symbol_table_push_(symtab, LNK_SymbolScopeIndex_Weak, node, hash);
} break;
case LNK_Symbol_Lazy: {
lnk_symbol_table_push_(symtab, LNK_SymbolScopeIndex_Lib, node, hash);
} break;
// symbols not supported
case LNK_Symbol_Undefined:
case LNK_Symbol_DefinedStatic: {
InvalidPath;
} break;
}
}
internal void
lnk_symbol_table_push_node(LNK_SymbolTable *symtab, LNK_SymbolNode *node)
{
U64 hash = lnk_symbol_table_hash(node->data->name);
lnk_symbol_table_push_node_hash(symtab, node, hash);
}
internal LNK_SymbolNode *
lnk_symbol_table_push(LNK_SymbolTable *symtab, LNK_Symbol *symbol)
{
LNK_SymbolNode *node = push_array(symtab->arena, LNK_SymbolNode, 1);
node->data = symbol;
lnk_symbol_table_push_node(symtab, node);
return node;
}
internal
THREAD_POOL_TASK_FUNC(lnk_lazy_symbol_inserter)
{
LNK_LazySymbolInserter *task = raw_task;
LNK_SymbolTable *symtab = task->symtab;
Rng1U64 range = task->range_arr[task_id];
for (U64 bucket_idx = range.min; bucket_idx < range.max; bucket_idx += 1) {
LNK_SymbolList *bucket = &task->bucket_arr[bucket_idx];
for (LNK_SymbolNode *curr = bucket->first, *next; curr != 0; curr = next) {
next = curr->next;
lnk_symbol_table_push_(symtab, LNK_SymbolScopeIndex_Lib, curr, curr->hash);
}
}
}
internal void
lnk_symbol_table_push_lazy_arr(TP_Context *tp, LNK_SymbolTable *symtab, LNK_Symbol *arr, U64 count)
{
Temp scratch = scratch_begin(0,0);
ProfBegin("Push Symbol Nodes");
LNK_SymbolNode *node_arr = push_array_no_zero(symtab->arena, LNK_SymbolNode, count);
for (U64 symbol_idx = 0; symbol_idx < count; symbol_idx += 1) {
LNK_SymbolNode *node = &node_arr[symbol_idx];
node->prev = node->next = 0;
node->data = &arr[symbol_idx];
}
ProfEnd();
ProfBegin("Hash Symbol Names");
lnk_symbol_node_array_hash(tp, node_arr, count);
ProfEnd();
ProfBegin("Populate Buckets");
LNK_SymbolList *bucket_arr = push_array(scratch.arena, LNK_SymbolList, symtab->bucket_count[LNK_SymbolScopeIndex_Lib]);
for (U64 symbol_idx = 0; symbol_idx < count; symbol_idx += 1) {
LNK_SymbolNode *symbol_node = &node_arr[symbol_idx];
U64 bucket_idx = symbol_node->hash % symtab->bucket_count[LNK_SymbolScopeIndex_Lib];
lnk_symbol_list_push_node(&bucket_arr[bucket_idx], symbol_node);
}
ProfEnd();
ProfBegin("Update Symbol Table");
LNK_LazySymbolInserter symbol_inserter;
symbol_inserter.symtab = symtab;
symbol_inserter.bucket_arr = bucket_arr;
symbol_inserter.range_arr = tp_divide_work(scratch.arena, symtab->bucket_count[LNK_SymbolScopeIndex_Lib], tp->worker_count);
tp_for_parallel(tp, 0, tp->worker_count, lnk_lazy_symbol_inserter, &symbol_inserter);
ProfEnd();
scratch_end(scratch);
}
internal void
lnk_symbol_table_push_list(LNK_SymbolTable *symtab, LNK_SymbolList *list)
{
ProfBeginFunction();
MemoryZeroStruct(list);
ProfEnd();
}
internal LNK_Symbol *
lnk_resolve_symbol(LNK_SymbolTable *symtab, LNK_Symbol *resolve_symbol)
{
LNK_Symbol *symbol = resolve_symbol;
B32 run_resolver;
do {
run_resolver = 0;
switch (symbol->type) {
case LNK_Symbol_Null: break;
case LNK_Symbol_Undefined: {
LNK_UndefinedSymbol *undef_symbol = &symbol->u.undefined;
LNK_Symbol *def = lnk_symbol_table_search(symtab, undef_symbol->scope_flags, symbol->name);
if (def) {
symbol = def;
run_resolver = 1;
}
} break;
case LNK_Symbol_Weak: {
LNK_WeakSymbol *weak = &symbol->u.weak;
LNK_Symbol *def = lnk_symbol_table_search(symtab, weak->scope_flags, symbol->name);
if (def) {
Assert(LNK_Symbol_IsDefined(def->type));
symbol = def;
} else {
symbol = symbol->u.weak.fallback_symbol;
}
run_resolver = 1;
} break;
case LNK_Symbol_DefinedStatic:
case LNK_Symbol_DefinedExtern:
case LNK_Symbol_DefinedInternal: {
/* resolved */
} break;
default: NotImplemented;
}
} while (run_resolver);
return symbol;
}
internal LNK_SymbolList
lnk_pop_comdat_chain(LNK_SymbolList *bucket, LNK_SymbolNode **cursor)
{
LNK_SymbolList chain_list = {0};
LNK_SymbolNode *leader_node = *cursor;
*cursor = (*cursor)->next;
lnk_symbol_list_remove(bucket, leader_node);
lnk_symbol_list_push_node(&chain_list, leader_node);
while (*cursor) {
LNK_SymbolNode *next = (*cursor)->next;
// symbols with identical names are stored in order
if (!str8_match(leader_node->data->name, (*cursor)->data->name, 0)) {
break;
}
// move node to chain list
lnk_symbol_list_remove(bucket, *cursor);
lnk_symbol_list_push_node(&chain_list, *cursor);
// advance
*cursor = next;
}
return chain_list;
}
internal LNK_SymbolNode *
lnk_fold_comdat_chain(LNK_SymbolList chain_list)
{
LNK_SymbolNode *lead_node = chain_list.first;
if (LNK_Symbol_IsDefined(lead_node->data->type)) {
LNK_Symbol *lead = lead_node->data;
if (lead->u.defined.value_type != LNK_DefinedSymbolValue_Chunk && chain_list.count > 1) {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "Unable to perfrom COMDAT fold on symbol %S, symbol must reference a section, defined in %S",
lead->name, lead->debug);
return 0;
}
}
for (LNK_SymbolNode *curr_node = lead_node->next; curr_node != 0; curr_node = curr_node->next) {
Assert(LNK_Symbol_IsDefined(lead_node->data->type));
Assert(LNK_Symbol_IsDefined(curr_node->data->type));
LNK_DefinedSymbol *lead_defined = &lead_node->data->u.defined;
LNK_DefinedSymbol *curr_defined = &curr_node->data->u.defined;
if (curr_defined->value_type != LNK_DefinedSymbolValue_Chunk) {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "Unable to perfrom COMDAT fold on symbol %S, symbol must reference a section, defined in %S",
curr_node->data->name, curr_node->data->debug);
return 0;
}
// There is no mentioning of this rule in PE spec, but according to comment from lld-link in 'handleComdatSelection':
// "cl.exe picks "any" for vftabels when building with /GR- and "largest" when building /GR.". However,
// chromium links '__src_ucrt_dll_is_in_use' from MSVCRT which is not a vftable but still requires selection override.
if ((curr_defined->u.selection == COFF_ComdatSelectType_ANY && lead_defined->u.selection == COFF_ComdatSelectType_LARGEST) ||
(curr_defined->u.selection == COFF_ComdatSelectType_LARGEST && lead_defined->u.selection == COFF_ComdatSelectType_ANY)) {
lead_defined->u.selection = COFF_ComdatSelectType_LARGEST;
curr_defined->u.selection = COFF_ComdatSelectType_LARGEST;
}
// COMDATs must have same selection rule
if (lead_defined->u.selection != curr_defined->u.selection) {
String8 curr_selection_str = coff_string_from_comdat_select_type(curr_defined->u.selection);
String8 lead_selection_str = coff_string_from_comdat_select_type(lead_defined->u.selection);
lnk_error(LNK_Warning_UnresolvedComdat,
"COMDAT selection conflict detected in symbol %S defined in %S (%S), leader selection %S from %S",
curr_node->data->name, curr_node->data->debug, curr_selection_str, lead_selection_str, lead_node->data->debug);
return 0;
}
switch (curr_defined->u.selection) {
case COFF_ComdatSelectType_NULL:
case COFF_ComdatSelectType_ANY: {
// both COMDATs are valid but to get smaller exe pick smallest
LNK_Chunk *lead_chunk = lead_defined->u.chunk;
LNK_Chunk *curr_chunk = curr_defined->u.chunk;
U64 lead_chunk_size = lnk_chunk_get_size(lead_chunk);
U64 curr_chunk_size = lnk_chunk_get_size(curr_chunk);
if (curr_chunk_size < lead_chunk_size) {
lead_node = curr_node;
}
} break;
case COFF_ComdatSelectType_NODUPLICATES: {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "%S: error: multiply defined symbol %S in %S.", curr_node->data->debug, curr_node->data->name, lead_node->data->debug);
} break;
case COFF_ComdatSelectType_SAME_SIZE: {
LNK_Chunk *lead_chunk = lead_defined->u.chunk;
LNK_Chunk *curr_chunk = curr_defined->u.chunk;
U64 lead_chunk_size = lnk_chunk_get_size(lead_chunk);
U64 curr_chunk_size = lnk_chunk_get_size(curr_chunk);
B32 is_same_size = (lead_chunk_size == curr_chunk_size);
if (!is_same_size) {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "%S: error: multiply defined symbol %S in %S.", curr_node->data->debug, curr_node->data->name, lead_node->data->debug);
}
} break;
case COFF_ComdatSelectType_EXACT_MATCH: {
B32 is_exact_match = (lead_defined->u.check_sum == curr_defined->u.check_sum);
if (!is_exact_match) {
lnk_error(LNK_Error_MultiplyDefinedSymbol, "%S: error: multiply defined symbol %S in %S.", curr_node->data->debug, curr_node->data->name, lead_node->data->debug);
}
} break;
case COFF_ComdatSelectType_LARGEST: {
LNK_Chunk *lead_chunk = lead_defined->u.chunk;
LNK_Chunk *curr_chunk = curr_defined->u.chunk;
U64 lead_chunk_size = lnk_chunk_get_size(lead_chunk);
U64 curr_chunk_size = lnk_chunk_get_size(curr_chunk);
if (lead_chunk_size > curr_chunk_size) {
lead_node = curr_node;
}
} break;
case COFF_ComdatSelectType_ASSOCIATIVE: {
// ignore
} break;
}
}
// rewire chunks so they point to COMDAT leader
for (LNK_SymbolNode *curr_node = chain_list.first; curr_node != 0; curr_node = curr_node->next) {
if (curr_node == lead_node) {
continue;
}
LNK_DefinedSymbol *curr_defined = &curr_node->data->u.defined;
LNK_Chunk *curr_chunk = curr_defined->u.chunk;
// copy offset because after folding COMDATS we might end
// up with larger sized chunk and, for instance, a vftable
// might have a function pointer preceeding lead symbol
curr_defined->u.chunk = lead_node->data->u.defined.u.chunk;
curr_defined->u.chunk_offset = lead_node->data->u.defined.u.chunk_offset;
// discard chunk from output
curr_chunk->is_discarded = 1;
// static symbols that are not part of obj's symbol table might point to discarded chunk
curr_chunk->ref = lead_node->data->u.defined.u.chunk->ref;
}
return lead_node;
}
internal
THREAD_POOL_TASK_FUNC(lnk_comdat_folder)
{
LNK_ComdatFolder *task = raw_task;
LNK_SymbolTable *symtab = task->symtab;
Rng1U64 range = task->range_arr[task_id];
for (U64 bucket_idx = range.min; bucket_idx < range.max; ++bucket_idx) {
LNK_SymbolList *bucket = &symtab->buckets[LNK_SymbolScopeIndex_Defined][bucket_idx];
LNK_SymbolList leader_list = {0};
LNK_SymbolNode *curr = bucket->first;
while (curr) {
LNK_SymbolList chain_list = lnk_pop_comdat_chain(bucket, &curr);
LNK_SymbolNode *leader_node = lnk_fold_comdat_chain(chain_list);
if (leader_node) {
lnk_symbol_list_push_node(&leader_list, leader_node);
}
}
Assert(bucket->count == 0);
*bucket = leader_list;
}
}
internal void
lnk_fold_comdat_chunks(TP_Context *tp, LNK_SymbolTable *symtab)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
LNK_ComdatFolder folder = {0};
folder.symtab = symtab;
folder.range_arr = tp_divide_work(scratch.arena, symtab->bucket_count[LNK_SymbolScopeIndex_Defined], tp->worker_count);
tp_for_parallel(tp, 0, tp->worker_count, lnk_comdat_folder, &folder);
scratch_end(scratch);
ProfEnd();
}
+253
View File
@@ -0,0 +1,253 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum
{
LNK_SymbolScopeIndex_Defined,
LNK_SymbolScopeIndex_Internal, // symbols defined by linker
LNK_SymbolScopeIndex_Weak,
LNK_SymbolScopeIndex_Lib,
LNK_SymbolScopeIndex_Count
} LNK_SymbolScopeIndex;
enum
{
LNK_SymbolScopeFlag_Defined = 1,
LNK_SymbolScopeFlag_Internal = 2,
LNK_SymbolScopeFlag_Weak = 4,
LNK_SymbolScopeFlag_Lib = 8,
LNK_SymbolScopeFlag_Main = LNK_SymbolScopeFlag_Defined | LNK_SymbolScopeFlag_Weak,
LNK_SymbolScopeFlag_All = LNK_SymbolScopeFlag_Defined | LNK_SymbolScopeFlag_Weak | LNK_SymbolScopeFlag_Lib | LNK_SymbolScopeFlag_Internal
};
typedef U64 LNK_SymbolScopeFlags;
typedef enum
{
LNK_DefinedSymbolVisibility_Static,
LNK_DefinedSymbolVisibility_Extern,
LNK_DefinedSymbolVisibility_Internal,
} LNK_DefinedSymbolVisibility;
enum
{
LNK_DefinedSymbolFlag_IsFunc = (1 << 0),
LNK_DefinedSymbolFlag_IsThunk = (1 << 1),
};
typedef U64 LNK_DefinedSymbolFlags;
typedef enum
{
LNK_DefinedSymbolValue_Null,
LNK_DefinedSymbolValue_Chunk,
LNK_DefinedSymbolValue_VA
} LNK_DefinedSymbolValueType;
typedef struct LNK_DefinedSymbol
{
LNK_DefinedSymbolFlags flags;
LNK_DefinedSymbolValueType value_type;
union {
struct {
LNK_Chunk *chunk;
U64 chunk_offset;
U32 check_sum;
COFF_ComdatSelectType selection;
};
U64 va;
} u;
} LNK_DefinedSymbol;
typedef struct LNK_WeakSymbol
{
LNK_SymbolScopeFlags scope_flags;
COFF_WeakExtType lookup_type;
struct LNK_Symbol *fallback_symbol;
} LNK_WeakSymbol;
typedef struct LNK_UndefinedSymbol
{
LNK_SymbolScopeFlags scope_flags;
} LNK_UndefinedSymbol;
typedef struct LNK_LazySymbol
{
struct LNK_Lib *lib;
U64 member_offset;
} LNK_LazySymbol;
#define LNK_Symbol_IsDefined(type) ((type) == LNK_Symbol_DefinedStatic || (type) == LNK_Symbol_DefinedExtern || (type) == LNK_Symbol_DefinedInternal)
typedef enum
{
LNK_Symbol_Null,
LNK_Symbol_DefinedStatic,
LNK_Symbol_DefinedExtern,
LNK_Symbol_DefinedInternal,
LNK_Symbol_Weak,
LNK_Symbol_Lazy,
LNK_Symbol_Undefined,
} LNK_SymbolType;
#define LNK_DEBUG_SYMBOLS 1
typedef struct LNK_Symbol
{
String8 name;
LNK_SymbolType type;
union {
LNK_DefinedSymbol defined;
LNK_WeakSymbol weak;
LNK_UndefinedSymbol undefined;
LNK_LazySymbol lazy;
} u;
#if LNK_DEBUG_SYMBOLS
String8 debug;
#endif
} LNK_Symbol;
typedef struct LNK_SymbolNode
{
struct LNK_SymbolNode *next;
struct LNK_SymbolNode *prev;
U64 hash;
LNK_Symbol *data;
} LNK_SymbolNode;
typedef struct LNK_SymbolList
{
U64 count;
LNK_SymbolNode *first;
LNK_SymbolNode *last;
} LNK_SymbolList;
typedef struct LNK_SymbolNodeArray
{
U64 count;
LNK_SymbolNode **v;
} LNK_SymbolNodeArray;
typedef struct LNK_SymbolArray
{
U64 count;
LNK_Symbol *v;
} LNK_SymbolArray;
typedef struct LNK_SymbolTable
{
Arena *arena;
U64 bucket_count[LNK_SymbolScopeIndex_Count];
LNK_SymbolList *buckets[LNK_SymbolScopeIndex_Count];
} LNK_SymbolTable;
////////////////////////////////
// parallel for wrappers
typedef struct
{
LNK_Symbol *symbol_arr;
Rng1U64 *range_arr;
U64 *hash_arr;
} LNK_SymbolNameHasher;
typedef struct
{
LNK_SymbolNode **input_arr;
Rng1U64 *range_arr;
} LNK_SymbolNodePtrHasher;
typedef struct
{
LNK_SymbolNode *input_arr;
Rng1U64 *range_arr;
} LNK_SymbolNodeHasher;
typedef struct
{
LNK_SymbolTable *symtab;
LNK_SymbolList *bucket_arr;
Rng1U64 *range_arr;
} LNK_DefinedSymbolInserter;
typedef struct
{
LNK_SymbolTable *symtab;
LNK_SymbolList *bucket_arr;
Rng1U64 *range_arr;
} LNK_LazySymbolInserter;
typedef struct
{
LNK_SymbolTable *symtab;
Rng1U64 *range_arr;
} LNK_ComdatFolder;
////////////////////////////////
extern LNK_Symbol g_null_symbol;
extern LNK_Symbol *g_null_symbol_ptr;
internal void lnk_init_symbol(LNK_Symbol *symbol, String8 name, LNK_SymbolType type);
internal void lnk_init_defined_symbol(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags);
internal void lnk_init_defined_symbol_chunk(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, LNK_Chunk *chunk, U64 offset, COFF_ComdatSelectType selection, U32 check_sum);
internal void lnk_init_defined_symbol_va(LNK_Symbol *symbol, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, U64 va);
internal void lnk_init_undefined_symbol(LNK_Symbol *symbol, String8 name, LNK_SymbolScopeFlags scope_flags);
internal void lnk_init_weak_symbol(LNK_Symbol *symbol, String8 name, COFF_WeakExtType lookup, LNK_Symbol *fallback);
internal LNK_Symbol * lnk_make_defined_symbol(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags);
internal LNK_Symbol * lnk_make_defined_symbol_chunk(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, LNK_Chunk *chunk, U64 offset, COFF_ComdatSelectType selection, U32 check_sum);
internal LNK_Symbol * lnk_make_defined_symbol_va(Arena *arena, String8 name, LNK_DefinedSymbolVisibility visibility, LNK_DefinedSymbolFlags flags, U64 va);
internal LNK_Symbol * lnk_make_undefined_symbol(Arena *arena, String8 name, LNK_SymbolScopeFlags scope_flags);
internal LNK_Symbol * lnk_make_weak_symbol(Arena *arena, String8 name, COFF_WeakExtType lookup, LNK_Symbol *fallback);
internal LNK_Symbol * lnk_make_lazy_symbol(Arena *arena, String8 name, struct LNK_Lib *lib, U64 member_offset);
#if LNK_DEBUG_SYMBOLS
#define lnk_symbol_set_debugf(a, s, fmt, ...) do { (s)->debug = push_str8f((a), fmt, __VA_ARGS__); } while (0)
#define lnk_symbol_set_debug(s, str) do { (s)->debug = str; } while (0)
#else
#define lnk_symbol_set_debugf(...)
#define lnk_symbol_set_debug(...)
#endif
internal LNK_Chunk * lnk_defined_symbol_get_chunk(LNK_DefinedSymbol *symbol);
internal void lnk_symbol_update_chunk_ref(LNK_Symbol *symbol, U64 src_sect_id, U64 dst_sect_id, U64 *id_map, U64 id_count);
internal void lnk_symbol_list_push_node(LNK_SymbolList *list, LNK_SymbolNode *node);
internal LNK_SymbolNode * lnk_symbol_list_push(Arena *arena, LNK_SymbolList *list, LNK_Symbol *symbol);
internal void lnk_symbol_list_push_list(LNK_SymbolList *list, LNK_SymbolList *to_push);
internal void lnk_symbol_list_insert_after(LNK_SymbolList *list, LNK_SymbolNode *node, LNK_SymbolNode *insert);
internal LNK_SymbolNode * lnk_symbol_list_pop_node(LNK_SymbolList *list);
internal LNK_Symbol * lnk_symbol_list_pop(LNK_SymbolList *list);
internal void lnk_symbol_list_remove(LNK_SymbolList *list, LNK_SymbolNode *node);
internal void lnk_symbol_list_concat_in_place(LNK_SymbolList *list, LNK_SymbolList *to_concat);
internal LNK_SymbolNodeArray lnk_symbol_node_array_from_list(Arena *arena, LNK_SymbolList list);
internal LNK_SymbolList lnk_symbol_list_from_array(Arena *arena, LNK_SymbolArray arr);
internal LNK_SymbolNodeArray lnk_symbol_node_array_from_list(Arena *arena, LNK_SymbolList list);
internal LNK_SymbolArray lnk_symbol_array_from_list(Arena *arena, LNK_SymbolList list);
internal LNK_Symbol * lnk_symbol_array_search(LNK_SymbolArray symarr, String8 name, StringMatchFlags flags);
internal U64 * lnk_symbol_array_hash(TP_Context *tp, Arena *arena, LNK_Symbol *arr, U64 count);
internal LNK_SymbolTable * lnk_symbol_table_alloc(void);
internal LNK_SymbolTable * lnk_symbol_table_alloc_ex(U64 defined_cap, U64 internal_cap, U64 weak_cap, U64 lib_cap);
internal void lnk_symbol_table_release(LNK_SymbolTable **symtab);
internal U64 lnk_symbol_table_hash(String8 string);
internal LNK_SymbolNode * lnk_symbol_table_search_bucket(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope_idx, U64 bucket_idx, String8 name, U64 hash);
internal LNK_SymbolNode * lnk_symbol_table_search_node_hash(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, String8 name, U64 hash);
internal LNK_SymbolNode * lnk_symbol_table_search_node(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope, String8 name);
internal LNK_Symbol * lnk_symbol_table_search(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, String8 name);
internal LNK_Symbol * lnk_symbol_table_searchf(LNK_SymbolTable *symtab, LNK_SymbolScopeFlags scope_flags, char *fmt, ...);
internal void lnk_symbol_table_push_node_hash(LNK_SymbolTable *symtab, LNK_SymbolNode *node, U64 hash);
internal void lnk_symbol_table_push_node(LNK_SymbolTable *symtab, LNK_SymbolNode *node);
internal LNK_SymbolNode * lnk_symbol_table_push(LNK_SymbolTable *symtab, LNK_Symbol *symbol);
internal void lnk_symbol_table_push_lazy_arr(TP_Context *tp, LNK_SymbolTable *symtab, LNK_Symbol *arr, U64 count);
internal void lnk_symbol_table_remove(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex scope, String8 name);
internal void lnk_symbol_table_replace(LNK_SymbolTable *symtab, LNK_SymbolScopeIndex iscope, LNK_Symbol *symbol);
internal LNK_Symbol * lnk_resolve_symbol(LNK_SymbolTable *symtab, LNK_Symbol *resolve_symbol);
internal LNK_SymbolList lnk_pop_comdat_chain(LNK_SymbolList *bucket, LNK_SymbolNode **cursor);
internal LNK_SymbolNode * lnk_fold_comdat_chain(LNK_SymbolList chain_list);
internal void lnk_fold_comdat_chunks(TP_Context *tp, LNK_SymbolTable *symtab);
+31
View File
@@ -0,0 +1,31 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
global LNK_Timer g_timers[LNK_Timer_Count];
internal void
lnk_timer_begin(LNK_TimerType timer)
{
g_timers[timer].begin = os_now_microseconds();
}
internal void
lnk_timer_end(LNK_TimerType timer)
{
g_timers[timer].end = os_now_microseconds();
}
internal String8
lnk_string_from_timer_type(LNK_TimerType type)
{
switch (type) {
case LNK_Timer_Image: return str8_lit("Image");
case LNK_Timer_Pdb: return str8_lit("PDB");
case LNK_Timer_Rdi: return str8_lit("RDI");
case LNK_Timer_Lib: return str8_lit("Lib");
case LNK_Timer_Debug: return str8_lit("Debug");
default: InvalidPath;
}
return str8_zero();
}
+24
View File
@@ -0,0 +1,24 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
typedef enum LNK_TimerType
{
LNK_Timer_Image,
LNK_Timer_Pdb,
LNK_Timer_Rdi,
LNK_Timer_Lib,
LNK_Timer_Debug,
LNK_Timer_Count
} LNK_TimerType;
typedef struct LNK_Timer
{
U64 begin;
U64 end;
} LNK_Timer;
internal void lnk_timer_begin(LNK_TimerType timer);
internal void lnk_timer_end(LNK_TimerType timer);
+188
View File
@@ -0,0 +1,188 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal
THREAD_POOL_TASK_FUNC(os_data_size_from_file_path_task)
{
OS_DataSizeFromFilePathTask *task = raw_task;
String8 path = task->path_arr.v[task_id];
OS_Handle handle = os_file_open(OS_AccessFlag_Read|OS_AccessFlag_ShareRead, path);
FileProperties props = os_properties_from_file(handle);
task->handle_arr[task_id] = handle;
task->size_arr[task_id] = props.size;
}
internal
THREAD_POOL_TASK_FUNC(os_data_from_file_path_task)
{
OS_DataFromFilePathTask *task = raw_task;
OS_Handle handle = task->handle_arr[task_id];
U64 size = task->size_arr[task_id];
U8 *buffer = task->buffer + task->off_arr[task_id];
U64 read_size = os_file_read(handle, rng_1u64(0, size), buffer);
Assert(read_size == size);
task->data_arr.v[task_id] = str8(buffer, read_size);
os_file_close(handle);
}
internal String8Array
os_data_from_file_path_parallel(TP_Context *tp, Arena *arena, String8Array path_arr)
{
Temp scratch = scratch_begin(&arena,1);
OS_Handle *handle_arr = push_array_no_zero(scratch.arena, OS_Handle, path_arr.count);
U64 *size_arr = push_array_no_zero(scratch.arena, U64, path_arr.count);
U64 *off_arr = push_array_no_zero(scratch.arena, U64, path_arr.count);
// open handles and get sizes
OS_DataSizeFromFilePathTask sizer;
sizer.path_arr = path_arr;
sizer.size_arr = size_arr;
sizer.handle_arr = handle_arr;
tp_for_parallel(tp, 0, path_arr.count, os_data_size_from_file_path_task, &sizer);
// compute file buffer size
U64 total_data_size = sum_array_u64(path_arr.count, sizer.size_arr);
// assign offsets into file buffer
MemoryCopyTyped(off_arr, sizer.size_arr, path_arr.count);
counts_to_offsets_array_u64(path_arr.count, off_arr);
// read files and close handles
OS_DataFromFilePathTask reader;
reader.data_arr = str8_array_reserve(arena, path_arr.count);
reader.handle_arr = handle_arr;
reader.size_arr = size_arr;;
reader.off_arr = off_arr;
reader.buffer = push_array_no_zero(arena, U8, total_data_size);
tp_for_parallel(tp, 0, path_arr.count, os_data_from_file_path_task, &reader);
String8Array result = {0};
result.count = path_arr.count;
result.v = reader.data_arr.v;
scratch_end(scratch);
return result;
}
internal String8List
os_file_search(Arena *arena, String8List dir_list, String8 file_path)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
String8List match_list; MemoryZeroStruct(&match_list);
if (os_file_path_exists(file_path)) {
String8 str = push_str8_copy(arena, file_path);
str8_list_push(arena, &match_list, str);
}
PathStyle file_path_style = path_style_from_str8(file_path);
B32 is_relative = file_path_style != PathStyle_WindowsAbsolute &&
file_path_style != PathStyle_UnixAbsolute;
if (is_relative) {
for (String8Node *i = dir_list.first; i != 0; i = i->next) {
String8List path_list = {0};
str8_list_push(scratch.arena, &path_list, i->string);
str8_list_push(scratch.arena, &path_list, file_path);
String8 path = str8_path_list_join_by_style(scratch.arena, &path_list, PathStyle_SystemAbsolute);
B32 file_exists = os_file_path_exists(path);
if (file_exists) {
B32 is_unique = 1;
OS_FileID file_id = os_id_from_file_path(path);
for (String8Node *k = match_list.first; k != 0; k = k->next) {
OS_FileID test_id = os_id_from_file_path(k->string);
int cmp = os_file_id_compare(test_id, file_id) != 0;
if (cmp == 0) {
is_unique = 0;
break;
}
}
if (is_unique) {
String8 str = push_str8_copy(arena, path);
str8_list_push(arena, &match_list, str);
}
}
}
}
scratch_end(scratch);
ProfEnd();
return match_list;
}
static struct
{
String8 string;
OperatingSystem os;
} g_os_map[] = {
{ str8_lit_comp("windows"), OperatingSystem_Windows, },
{ str8_lit_comp("linux"), OperatingSystem_Linux, },
{ str8_lit_comp("mac"), OperatingSystem_Mac, },
};
internal OperatingSystem
operating_system_from_string(String8 string)
{
for (U64 i = 0; i < ArrayCount(g_os_map); ++i) {
if (str8_match(g_os_map[i].string, string, StringMatchFlag_CaseInsensitive)) {
return g_os_map[i].os;
}
}
return OperatingSystem_Null;
}
internal B32
os_try_guid_from_string(String8 string, OS_Guid *guid_out)
{
Temp scratch = scratch_begin(0,0);
B32 is_parsed = 0;
String8List list = str8_split_by_string_chars(scratch.arena, string, str8_lit("-"), StringSplitFlag_KeepEmpties);
if (list.node_count == 5) {
String8 data1_str = list.first->string;
String8 data2_str = list.first->next->string;
String8 data3_str = list.first->next->next->string;
String8 data4_hi_str = list.first->next->next->next->string;
String8 data4_lo_str = list.first->next->next->next->next->string;
if (str8_is_integer(data1_str, 16) &&
str8_is_integer(data2_str, 16) &&
str8_is_integer(data3_str, 16) &&
str8_is_integer(data4_hi_str, 16) &&
str8_is_integer(data4_lo_str, 16)) {
U64 data1 = u64_from_str8(data1_str, 16);
U64 data2 = u64_from_str8(data2_str, 16);
U64 data3 = u64_from_str8(data3_str, 16);
U64 data4_hi = u64_from_str8(data4_hi_str, 16);
U64 data4_lo = u64_from_str8(data4_lo_str, 16);
if (data1 <= max_U32 &&
data2 <= max_U16 &&
data3 <= max_U16 &&
data4_hi <= max_U16 &&
data4_lo <= 0xffffffffffff) {
guid_out->data1 = (U32)data1;
guid_out->data2 = (U16)data2;
guid_out->data3 = (U16)data3;
U64 data4 = (data4_hi << 48) | data4_lo;
MemoryCopy(&guid_out->data4[0], &data4, sizeof(data4));
is_parsed = 1;
}
}
}
scratch_end(scratch);
return is_parsed;
}
internal OS_Guid
os_guid_from_string(String8 string)
{
OS_Guid guid = {0};
os_try_guid_from_string(string, &guid);
return guid;
}
+39
View File
@@ -0,0 +1,39 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#if OS_WINDOWS
# include "os_core_win32.c"
#else
# error "undefined OS"
#endif
typedef struct
{
String8Array path_arr;
OS_Handle *handle_arr;
U64 *size_arr;
} OS_DataSizeFromFilePathTask;
typedef struct
{
String8Array data_arr;
OS_Handle *handle_arr;
U64 *size_arr;
U64 *off_arr;
U8 *buffer;
} OS_DataFromFilePathTask;
internal String8Array os_data_from_file_path_parallel(TP_Context *tp, Arena *arena, String8Array path_arr);
internal String8List os_file_search(Arena *arena, String8List dir_list, String8 file_path);
internal B32 os_folder_path_exists(String8 path);
internal OperatingSystem operating_system_from_string(String8 string);
internal B32 os_set_large_pages(B32 toggle);
internal U32 os_get_process_start_time_unix(void);
internal B32 os_try_guid_from_string(String8 string, OS_Guid *guid_out);
internal OS_Guid os_guid_from_string(String8 string);
+168
View File
@@ -0,0 +1,168 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U32
w32_unix_time_from_file_time(FILETIME file_time)
{
U64 win32_time = ((U64)file_time.dwHighDateTime << 32) | file_time.dwLowDateTime;
U64 unix_time64 = ((win32_time - 0x19DB1DED53E8000ULL) / 10000000);
Assert(unix_time64 <= max_U32);
U32 unix_time32 = (U32)unix_time64;
return unix_time32;
}
internal B32
os_w32_has_path_volume_prefix(String8 path)
{
if (path.size >= 2) {
U8 *ptr = path.str;
U8 *opl = path.str + path.size;
UnicodeDecode a = utf8_decode(ptr, (U64)(opl-ptr));
ptr += a.inc;
UnicodeDecode b = utf8_decode(ptr, (U64)(opl-ptr));
return a.codepoint < max_U8 && char_is_alpha(a.codepoint) && b.codepoint == ':';
}
return 0;
}
internal B32
os_w32_has_device_prefix(String8 path)
{
if (path.size >= 3) {
U8 *ptr = path.str;
U8 *opl = path.str + path.size;
UnicodeDecode a = utf8_decode(ptr, (U64)(opl-ptr));
ptr += a.inc;
UnicodeDecode b = utf8_decode(ptr, (U64)(opl-ptr));
ptr += b.inc;
UnicodeDecode c = utf8_decode(ptr, (U64)(opl-ptr));
return a.codepoint == '\\' && b.codepoint == '\\' && (c.codepoint == '?' || c.codepoint == '.');
}
return 0;
}
internal B32
os_w32_has_unc_prefix(String8 path)
{
if (path.size >= 2) {
U8 *ptr = path.str;
U8 *opl = path.str + path.size;
UnicodeDecode a = utf8_decode(ptr, (U64)(opl-ptr));
ptr += a.inc;
UnicodeDecode b = utf8_decode(ptr, (U64)(opl-ptr));
return a.codepoint == '\\' && b.codepoint == '\\';
}
return 0;
}
internal B32
os_w32_has_root_drive_prefix(String8 path)
{
if (path.size >= 1) {
UnicodeDecode a = utf8_decode(path.str, path.size);
return a.codepoint == '\\';
}
return 0;
}
internal B32
os_w32_is_path_relative_current_directory(String8 path)
{
if (os_w32_has_path_volume_prefix(path)) {
return 0;
}
if (os_w32_has_device_prefix(path)) {
return 0;
}
if (os_w32_has_unc_prefix(path)) {
return 0;
}
if (os_w32_has_root_drive_prefix(path)) {
return 0;
}
return 1;
}
internal String8
os_make_full_path(Arena *arena, String8 path)
{
String8 full_path;
if (os_w32_is_path_relative_current_directory(path)) {
Temp scratch = scratch_begin(&arena, 1);
String8 current_dir = os_get_current_path(scratch.arena);
String8List list = {0};
str8_list_push(scratch.arena, &list, current_dir);
str8_list_push(scratch.arena, &list, path);
String8 temp_full_path = str8_list_join(scratch.arena, &list, &(StringJoin){ .sep = str8_lit_comp("\\") });
String8List split_full_path = str8_split_path(scratch.arena, temp_full_path);
str8_path_list_resolve_dots_in_place(&split_full_path, PathStyle_WindowsAbsolute);
full_path = str8_list_join(arena, &split_full_path, &(StringJoin){ .sep = str8_lit_comp("\\") });
scratch_end(scratch);
} else {
full_path = push_str8_copy(arena, path);
}
return full_path;
}
internal B32
os_folder_path_exists(String8 path)
{
Temp scratch = scratch_begin(0,0);
String8 actual_path = path;
if (os_w32_is_path_relative_current_directory(path)) {
String8 current = os_get_current_path(scratch.arena);
String8List list = {0};
str8_list_push(scratch.arena, &list, current);
str8_list_push(scratch.arena, &list, path);
StringJoin join = { .sep = str8_lit_comp("\\") };
actual_path =str8_list_join(scratch.arena, &list, &join);
}
String16 path16 = str16_from_8(scratch.arena, actual_path);
DWORD attributes = GetFileAttributesW((WCHAR *)path16.str);
B32 exists = (attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY);
scratch_end(scratch);
return exists;
}
internal B32
os_set_large_pages(B32 toggle)
{
B32 is_ok = 0;
HANDLE token;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
{
LUID luid;
if(LookupPrivilegeValue(0, SE_LOCK_MEMORY_NAME, &luid))
{
TOKEN_PRIVILEGES priv;
priv.PrivilegeCount = 1;
priv.Privileges[0].Luid = luid;
priv.Privileges[0].Attributes = toggle ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED;
if (AdjustTokenPrivileges(token, 0, &priv, sizeof(priv), 0, 0) == ERROR_SUCCESS) {
is_ok = 1;
}
}
CloseHandle(token);
}
return is_ok;
}
internal U32
os_get_process_start_time_unix(void)
{
HANDLE handle = GetCurrentProcess();
FILETIME start_time = {0};
FILETIME exit_time;
FILETIME kernel_time;
FILETIME user_time;
if (GetProcessTimes(handle, &start_time, &exit_time, &kernel_time, &user_time)) {
return w32_unix_time_from_file_time(start_time);
}
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#include "core/os_core.c"
#if OS_WINDOWS
//# include "core/win32/os_core_win32.c"
#elif OS_LINUX
//# include "core/linux/os_core_linux.c"
#else
# error no OS layer setup
#endif
+15
View File
@@ -0,0 +1,15 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#include "core/os_core.h"
#if OS_WINDOWS
//# include "core/win32/os_core_win32.h"
#elif OS_LINUX
//# include "core/linux/os_core_linux.h"
#else
# error no OS layer setup
#endif
+84
View File
@@ -0,0 +1,84 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal String8
make_file_name_with_ext(Arena *arena, String8 file_name, String8 ext)
{
String8 file_name_no_ext = str8_chop_last_dot(file_name);
String8 result = push_str8f(arena, "%S.%S", file_name_no_ext, ext);
return result;
}
internal String8
make_file_path_with_ext(Arena *arena, String8 file_name, String8 ext)
{
Temp scratch = scratch_begin(&arena, 1);
String8 curr = os_get_current_path(scratch.arena);
String8 name = make_file_name_with_ext(scratch.arena, str8_skip_last_slash(file_name), ext);
String8List list = {0};
str8_list_push(scratch.arena, &list, curr);
str8_list_push(scratch.arena, &list, name);
String8 result = str8_path_list_join_by_style(arena, &list, PathStyle_SystemAbsolute);
scratch_end(scratch);
return result;
}
internal String8
path_char_from_style(PathStyle style)
{
String8 result = str8_zero();
switch (style) {
case PathStyle_Null: break;
case PathStyle_Relative: break;
case PathStyle_WindowsAbsolute: result = str8_lit("\\"); break;
case PathStyle_UnixAbsolute: result = str8_lit("/"); break;
}
return result;
}
internal String8
path_convert_slashes(Arena *arena, String8 path, PathStyle path_style)
{
Temp scratch = scratch_begin(&arena, 1);
String8List list = str8_split_path(scratch.arena, path);
StringJoin join = {0};
join.sep = path_char_from_style(path_style);
String8 result = str8_list_join(arena, &list, &join);
scratch_end(scratch);
return result;
}
internal String8
path_canon_from_regular_path(Arena *arena, String8 path)
{
Temp scratch = scratch_begin(&arena, 1);
String8 result;
result = lower_from_str8(scratch.arena, path);
result = path_convert_slashes(arena, result, PathStyle_UnixAbsolute);
scratch_end(scratch);
return result;
}
struct {
String8 string;
PathStyle path_style;
} g_path_style_map[] = {
{ str8_lit_comp("windows"), PathStyle_WindowsAbsolute },
{ str8_lit_comp("unix"), PathStyle_UnixAbsolute },
{ str8_lit_comp("system"), PathStyle_SystemAbsolute },
};
internal PathStyle
path_style_from_string(String8 string)
{
for (U64 i = 0; i < ArrayCount(g_path_style_map); ++i) {
if (str8_match(g_path_style_map[i].string, string, StringMatchFlag_CaseInsensitive)) {
return g_path_style_map[i].path_style;
}
}
return PathStyle_Null;
}
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
internal String8 make_file_name_with_ext(Arena *arena, String8 file_name, String8 ext);
internal String8 make_file_path_with_ext(Arena *arena, String8 file_name, String8 ext);
internal String8 path_convert_slashes(Arena *arena, String8 path, PathStyle path_style);
internal String8 path_canon_from_regular_path(Arena *arena, String8 path);
internal PathStyle path_style_from_string(String8 string);
File diff suppressed because it is too large Load Diff
+202
View File
@@ -0,0 +1,202 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#pragma once
#define MSF_PAGE_STATE_FREE 1
#define MSF_PAGE_STATE_ALLOC 0
#define MSF_FPM0 1
#define MSF_FPM1 2
#define MSF_DEFAULT_PAGE_SIZE 4096
#define MSF_DEFAULT_FPM MSF_FPM0
typedef struct MSF_PageNumberArray
{
U64 count;
MSF_PageNumber *v;
} MSF_PageNumberArray;
typedef struct MSF_PageNode
{
struct MSF_PageNode *next;
struct MSF_PageNode *prev;
MSF_PageNumber pn;
} MSF_PageNode;
typedef struct MSF_PageList
{
MSF_PageNode *first;
MSF_PageNode *last;
MSF_UInt count;
} MSF_PageList;
typedef struct MSF_Stream
{
MSF_StreamNumber sn;
MSF_UInt size;
MSF_UInt pos;
MSF_PageNode *pos_page;
MSF_PageList page_list;
} MSF_Stream;
typedef struct MSF_StreamNode
{
struct MSF_StreamNode *next;
struct MSF_StreamNode *prev;
MSF_Stream data;
} MSF_StreamNode;
typedef struct MSF_StreamList
{
MSF_UInt count;
MSF_StreamNode *first;
MSF_StreamNode *last;
} MSF_StreamList;
typedef struct MSF_PageDataNode
{
struct MSF_PageDataNode *next;
struct MSF_PageDataNode *prev;
U8 *data;
} MSF_PageDataNode;
typedef struct MSF_PageDataList
{
MSF_PageDataNode *first;
MSF_PageDataNode *last;
MSF_UInt count;
} MSF_PageDataList;
typedef struct MSF_Context
{
Arena *arena;
MSF_UInt page_size;
MSF_UInt active_fpm;
MSF_UInt fpm_rover;
MSF_PageNumber page_count;
MSF_PageDataList page_data_list;
MSF_PageDataList page_data_pool;
MSF_PageList header_page_list;
MSF_PageList root_page_list;
MSF_PageList st_page_list;
MSF_PageList page_pool;
MSF_StreamList st;
} MSF_Context;
typedef enum MSF_Error
{
MSF_Error_OK,
// if you get this error this means stream table was divided into too many
// pages, and to fix this you need to bump up the page size
MSF_Error_STREAM_TABLE_HAS_TOO_MANY_PAGES,
MSF_OpenError_NOT_ENOUGH_BYTES_TO_READ_HEADER,
MSF_OpenError_INVALID_MAGIC,
MSF_OpenError_PAGE_SIZE_IS_NOT_POW2,
MSF_OpenError_INVALID_PAGE_SIZE,
MSF_OpenError_NOT_ENOUGH_PAGES_TO_INIT,
MSF_OpenError_INVALID_ROOT_STREAM_PAGE_NUMBER,
MSF_OpenError_UNABLE_TO_READ_STREAM_TABLE_PAGE_NUMBERS,
MSF_OpenError_STREAM_COUNT_OVERFLOW,
MSF_OpenError_UNABLE_TO_READ_STREAM_SIZES,
MSF_OpenError_INVALID_STREAM_TABLE,
MSF_OpenError_INVALID_ACTIVE_FPM,
MSF_OpenError_PAGE_COUNT_DOESNT_MATCH_DATA_SIZE,
MSF_BuildError_UNABLE_TO_WRITE_STREAM_TABLE,
MSF_BuildError_UNABLE_TO_WRITE_STREAM_TABLE_PAGE_NUMBER_DIRECTORY,
MSF_BuildError_UNABLE_TO_WRITE_ROOT_DIRECTORY,
MSF_BuildError_UNABLE_TO_WRITE_HEADER,
} MSF_Error;
////////////////////////////////
typedef struct
{
MSF_UInt page_size;
MSF_PageDataList page_data_list;
MSF_PageList page_list;
MSF_UInt stream_pos;
String8 data;
Rng1U64 *range_arr;
} MSF_WriteTask;
////////////////////////////////
internal MSF_Context * msf_alloc(MSF_UInt page_size, MSF_UInt active_fpm);
internal MSF_Error msf_open(String8 data, MSF_Context **msf_out);
internal void msf_release(MSF_Context **msf_ptr);
internal MSF_Error msf_build(MSF_Context *msf);
internal U64 msf_get_save_size(MSF_Context *msf);
internal String8List msf_get_page_data_nodes(Arena *arena, MSF_Context *msf);
internal B32 msf_save(MSF_Context *msf, void *buffer, U64 buffer_size);
internal MSF_Error msf_save_arena(Arena *arena, MSF_Context *msf, String8 *data_out);
internal MSF_StreamNode * msf_find_stream_node(MSF_Context *msf, MSF_StreamNumber sn);
internal MSF_Stream * msf_find_stream(MSF_Context *msf, MSF_StreamNumber sn);
internal B32 msf_grow(MSF_Context *msf, MSF_PageNumber page_count);
internal MSF_PageNumber * msf_alloc_pn_arr(Arena *arena, MSF_Context *msf, MSF_UInt alloc_count);
internal void msf_free_pn_arr(MSF_Context *msf, MSF_PageNumber *pn_arr, MSF_UInt pn_count);
internal MSF_PageList msf_alloc_pages(MSF_Context *msf, MSF_UInt alloc_count);
internal void msf_free_pages(MSF_Context *msf, MSF_PageList *page_list);
internal MSF_StreamNumber msf_stream_alloc_ex(MSF_Context *msf, MSF_UInt size);
internal MSF_StreamNumber msf_stream_alloc(MSF_Context *msf);
internal B32 msf_stream_free(MSF_Context *msf, MSF_StreamNumber sn);
internal B32 msf_stream_resize(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt new_size);
internal B32 msf_stream_resize_ex(MSF_Context *msf, MSF_Stream *stream, MSF_UInt size);
internal void msf_stream_set_size(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt size);
internal MSF_UInt msf_stream_get_size(MSF_Context *msf, MSF_StreamNumber sn);
internal MSF_UInt msf_stream_get_cap(MSF_Context *msf, MSF_StreamNumber);
internal MSF_UInt msf_stream_get_pos(MSF_Context *msf, MSF_StreamNumber sn);
internal void msf_stream_align(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt align);
internal B32 msf_stream_reserve(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt size);
internal B32 msf_stream_seek(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt new_pos);
internal B32 msf_stream_seek_start(MSF_Context *msf, MSF_StreamNumber sn);
internal B32 msf_stream_seek_end(MSF_Context *msf, MSF_StreamNumber sn);
internal MSF_UInt msf_stream_read(MSF_Context *msf, MSF_StreamNumber sn, void *dst, MSF_UInt dst_len);
internal String8 msf_stream_read_block(Arena *arena, MSF_Context *msf, MSF_StreamNumber sn, U64 block_size);
internal String8 msf_stream_read_string(Arena *arena, MSF_Context *msf, MSF_StreamNumber sn);
internal S8 msf_stream_read_s8(MSF_Context *msf, MSF_StreamNumber sn);
internal S16 msf_stream_read_s16(MSF_Context *msf, MSF_StreamNumber sn);
internal S32 msf_stream_read_s32(MSF_Context *msf, MSF_StreamNumber sn);
internal S64 msf_stream_read_s64(MSF_Context *msf, MSF_StreamNumber sn);
internal U8 msf_stream_read_u8(MSF_Context *msf, MSF_StreamNumber sn);
internal U16 msf_stream_read_u16(MSF_Context *msf, MSF_StreamNumber sn);
internal U32 msf_stream_read_u32(MSF_Context *msf, MSF_StreamNumber sn);
internal U64 msf_stream_read_u64(MSF_Context *msf, MSF_StreamNumber sn);
#define msf_stream_read_array(msf, sn, ptr, count) msf_stream_read(msf, sn, ptr, sizeof(*ptr) * (count))
#define msf_stream_read_struct(msf, sn, ptr) msf_stream_read_array(msf, sn, ptr, 1)
internal B32 msf_stream_write(MSF_Context *msf, MSF_StreamNumber sn, void *buffer, MSF_UInt buffer_size);
internal B32 msf_stream_write_string(MSF_Context *msf, MSF_StreamNumber sn, String8 string);
internal B32 msf_stream_write_list(MSF_Context *msf, MSF_StreamNumber sn, String8List list);
internal B32 msf_stream_write_uint(MSF_Context *msf, MSF_StreamNumber sn, MSF_UInt value);
internal B32 msf_stream_write_cstr(MSF_Context *msf, MSF_StreamNumber sn, String8 string);
internal B32 msf_stream_write_u8(MSF_Context *msf, MSF_StreamNumber sn, U8 value);
internal B32 msf_stream_write_u16(MSF_Context *msf, MSF_StreamNumber sn, U16 value);
internal B32 msf_stream_write_u32(MSF_Context *msf, MSF_StreamNumber sn, U32 value);
internal B32 msf_stream_write_u64(MSF_Context *msf, MSF_StreamNumber sn, U64 value);
internal B32 msf_stream_write_s8(MSF_Context *msf, MSF_StreamNumber sn, S8 value);
internal B32 msf_stream_write_s16(MSF_Context *msf, MSF_StreamNumber sn, S16 value);
internal B32 msf_stream_write_s32(MSF_Context *msf, MSF_StreamNumber sn, S32 value);
internal B32 msf_stream_write_s64(MSF_Context *msf, MSF_StreamNumber sn, S64 value);
internal B32 msf_stream_write_parallel(TP_Context *tp, MSF_Context *msf, MSF_StreamNumber sn, void *buffer, MSF_UInt buffer_size);
#define msf_stream_write_array(m, s, v, c) msf_stream_write(m, s, (void*)(v), sizeof(*(v)) * (c))
#define msf_stream_write_struct(m, s, v ) msf_stream_write_array(m, s, v, 1)
internal MSF_UInt msf_count_pages(MSF_UInt page_size, U64 data_size);
internal MSF_PageNumber msf_get_page_count_cap(MSF_PageDataList page_data_list, MSF_UInt page_size);
internal MSF_UInt msf_get_fpm_interval_correct(MSF_UInt page_size);
internal MSF_UInt msf_get_fpm_interval_wrong(MSF_UInt page_size);
internal MSF_UInt msf_get_fpm_idx_from_pn(MSF_UInt page_size, MSF_PageNumber pn);
internal MSF_UInt msf_get_fpm_page_bit_state(MSF_PageDataList page_data_list, MSF_UInt page_size, MSF_PageNumber active_fpm, MSF_PageNumber pn);
internal void msf_set_fpm_bit(MSF_PageDataList page_data_list, MSF_UInt page_size, MSF_PageNumber active_fpm, MSF_PageNumber pn, B32 state);
internal B32 msf_write(MSF_PageDataList page_data_list, MSF_UInt page_size, MSF_PageList page_list, MSF_UInt offset, void *buffer, MSF_UInt buffer_size);
internal MSF_UInt msf_read(MSF_PageDataList page_data_list, MSF_UInt page_size, MSF_PageList page_list, MSF_UInt offset, void *buffer, MSF_UInt buffer_size);
internal char * msf_error_to_string(MSF_Error code);
+34
View File
@@ -0,0 +1,34 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal U32
pdb_hash_udt(CV_UDTInfo udt_info, String8 data)
{
B32 is_fwdref = !!(udt_info.props & CV_TypeProp_FwdRef);
B32 is_scoped = !!(udt_info.props & CV_TypeProp_Scoped);
B32 has_unique_name = !!(udt_info.props & CV_TypeProp_HasUniqueName);
B32 is_anon = has_unique_name && cv_is_udt_name_anon(udt_info.name);
U32 hash = 0;
// dbi/tpi.cpp:1918
if (!is_fwdref && !is_scoped && !is_anon) {
hash = pdb_hash_v1(udt_info.name);
}
// dbi/tpi.cpp:1937
else if (!is_fwdref && has_unique_name && is_scoped && !is_anon) {
hash = pdb_hash_v1(udt_info.unique_name);
}
// dbi/tpi.cpp 1338
else {
hash = pdb_hash_v1(data);
}
return hash;
}
internal U32
pdb_crc32_from_string(String8 string)
{
return ~update_crc32(~0, string.str, string.size);
}

Some files were not shown because too many files have changed in this diff Show More