Skip to content

Commit

Permalink
cleanup import libraries handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalak committed Feb 1, 2023
1 parent 81aeaf7 commit 0567a26
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 187 deletions.
64 changes: 37 additions & 27 deletions bld/nwlib/c/coffwrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <error.h>
#include "coff.h"
#include "coffwrt.h"
#include "roundmac.h"

#include "clibext.h"

Expand Down Expand Up @@ -66,41 +67,50 @@ typedef struct name_len {
char *name;
} name_len;

static unsigned_32 CoffImportAxpText[] = {
0x277F0000, // ldah r27,hioff(r31)
0xA37B0000, // ldl r27,looff(r27)
0x6BFB0000 // jmp r31,0(r27)
/*
* ALPHA transfer code
*/
static unsigned_32 CoffImportAxpText[] = {
0x277F0000, // ldah r27,hioff(r31)
0xA37B0000, // ldl r27,looff(r27)
0x6BFB0000 // jmp r31,0(r27)
};

static unsigned_32 CoffImportMipsText[] = {
0x3C080000, // lui r8,hioff(r0)
0x8D080000, // lw r8,looff(r8)
0x01000008, // jr r8
/*
* MIPS transfer code
*/
static unsigned_32 CoffImportMipsText[] = {
0x3C080000, // lui r8,hioff(r0)
0x8D080000, // lw r8,looff(r8)
0x01000008, // jr r8
};

static unsigned_32 CoffImportPpcText[] = {
0x81620000, // lwz r11,[tocv]__imp_RtlMoveMemory(rtoc)
0x818B0000, // lwz r12,(r11)
0x90410004, // stw rtoc,0x4(sp)
0x7D8903A6, // mtctr r12
0x804B0004, // lwz rtoc,0x4(r11)
0x4E800420 // bctr
/*
* PPC transfer code
*/
static unsigned_32 CoffImportPpcText[] = {
0x81620000, // lwz r11,[tocv]__imp_RtlMoveMemory(rtoc)
0x818B0000, // lwz r12,(r11)
0x90410004, // stw rtoc,0x4(sp)
0x7D8903A6, // mtctr r12
0x804B0004, // lwz rtoc,0x4(r11)
0x4E800420 // bctr
};

static unsigned_8 CoffImportPpcPdata[] = {
0x00,0x00,0x00,0x00,
0x18,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x03,0x00,0x00,0x00,
0x0D,0x00,0x00,0x00
static unsigned_32 CoffImportPpcPdata[] = {
0x00000000,
0x00000018,
0x00000000,
0x00000003,
0x0000000D,
};

static unsigned_8 CoffImportX86Text[] = {
0xFF,0x25,0x00,0x00,0x00,0x00
static unsigned_8 CoffImportX64Text[] = {
0xFF,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

static unsigned_8 CoffImportX64Text[] = {
0xFF,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
static unsigned_8 CoffImportX86Text[] = {
0xFF,0x25,0x00,0x00,0x00,0x00
};

static void InitCoffFile( coff_lib_file *c_file )
Expand Down Expand Up @@ -388,7 +398,7 @@ static void WriteImportDescriptor( libfile io, sym_file *sfile, coff_lib_file *c
memcpy( buffer + 20, modName->name, modName->len + 1 );
AddCoffSymbol( c_file, buffer, 0x0, sec_num, COFF_IMAGE_SYM_TYPE_NULL, COFF_IMAGE_SYM_CLASS_EXTERNAL, 0 );
AddCoffSymbol( c_file, ".idata$2", 0xC0000040, sec_num, COFF_IMAGE_SYM_TYPE_NULL, COFF_IMAGE_SYM_CLASS_SECTION, 0 );
sec_num = AddCoffSection( c_file, ".idata$6", Round2( dllName->len + 1 ), 0, COFF_IMAGE_SCN_ALIGN_2BYTES
sec_num = AddCoffSection( c_file, ".idata$6", __ROUND_UP_SIZE_EVEN( dllName->len + 1 ), 0, COFF_IMAGE_SCN_ALIGN_2BYTES
| COFF_IMAGE_SCN_CNT_INITIALIZED_DATA | COFF_IMAGE_SCN_MEM_READ | COFF_IMAGE_SCN_MEM_WRITE );
AddCoffSymbol( c_file, ".idata$6", 0x0, sec_num, COFF_IMAGE_SYM_TYPE_NULL, COFF_IMAGE_SYM_CLASS_STATIC, 0 );
AddCoffSymbol( c_file, ".idata$4", 0xC0000040, 0, COFF_IMAGE_SYM_TYPE_NULL, COFF_IMAGE_SYM_CLASS_SECTION, 0 );
Expand Down Expand Up @@ -606,7 +616,7 @@ void CoffWriteImport( libfile io, sym_file *sfile, bool long_format )
| COFF_IMAGE_SCN_MEM_READ | COFF_IMAGE_SCN_MEM_WRITE );
AddCoffSymSec( &c_file, sec_num, COFF_IMAGE_COMDAT_SELECT_ASSOCIATIVE );
if( named_relocs ) {
sec_num = AddCoffSection( &c_file, ".idata$6", sizeof( ordinal ) + Round2( exportedName.len + 1 ),
sec_num = AddCoffSection( &c_file, ".idata$6", sizeof( ordinal ) + __ROUND_UP_SIZE_EVEN( exportedName.len + 1 ),
0, COFF_IMAGE_SCN_ALIGN_2BYTES | COFF_IMAGE_SCN_LNK_COMDAT
| COFF_IMAGE_SCN_CNT_INITIALIZED_DATA | COFF_IMAGE_SCN_MEM_READ
| COFF_IMAGE_SCN_MEM_WRITE );
Expand Down
17 changes: 9 additions & 8 deletions bld/nwlib/c/implib.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "wlib.h"
#include "ar.h"
#include "coff.h"
#include "roundmac.h"

#include "clibext.h"

Expand Down Expand Up @@ -758,11 +759,11 @@ size_t ElfImportSize( import_sym *import )
for( temp = import->u.elf.symlist; temp != NULL; temp = temp->next ) {
len += temp->len;
}
Round2var( len );
len = __ROUND_UP_SIZE_EVEN( len );
break;
case ELFRENAMED:
len += 0x21 + 1 + import->u.elf.symlist->len + import->u.elf.symlist->next->len;
Round2var( len );
len = __ROUND_UP_SIZE_EVEN( len );
break;
default:
break;
Expand Down Expand Up @@ -805,7 +806,7 @@ size_t CoffImportSize( import_sym *import )
+ opt_hdr_len // optional header
+ 2 * COFF_SECTION_HEADER_SIZE + // section table (headers)
+ 0x14 + 3 * COFF_RELOC_SIZE // section data
+ Round2( dll_len + 1 ) // section data
+ __ROUND_UP_SIZE_EVEN( dll_len + 1 ) // section data
+ 7 * COFF_SYM_SIZE // symbol table
+ 4 + mod_len + 21 + 25 + mod_len + 18 ); // string table
case NULL_IMPORT_DESCRIPTOR:
Expand All @@ -832,11 +833,11 @@ size_t CoffImportSize( import_sym *import )
}
ret = COFF_FILE_HEADER_SIZE
+ 4 * COFF_SECTION_HEADER_SIZE
+ 4 + COFF_RELOC_SIZE // idata$5
+ 4 + COFF_RELOC_SIZE // idata$4
+ 2 + Round2( exp_len + 1 ) // idata$6
+ 4 + COFF_RELOC_SIZE // idata$5
+ 4 + COFF_RELOC_SIZE // idata$4
+ 2 + __ROUND_UP_SIZE_EVEN( exp_len + 1 ) // idata$6
+ 11 * COFF_SYM_SIZE
+ 4 + mod_len + 21; // 21 = strlen("__IMPORT_DESCRIPTOR_") + 1
+ 4 + mod_len + 21; // 21 = strlen("__IMPORT_DESCRIPTOR_") + 1
} else {
ret = COFF_FILE_HEADER_SIZE
+ 3 * COFF_SECTION_HEADER_SIZE
Expand Down Expand Up @@ -926,7 +927,7 @@ void ElfWriteImport( libfile io, sym_file *sfile )
strtabsize += temp->len + 1;
}
padding = ( (strtabsize & 1) != 0 );
Round2var( strtabsize );
strtabsize = __ROUND_UP_SIZE_EVEN( strtabsize );
fillInU16( ElfProcessors[import->processor], &(ElfBase[0x12]) );
fillInU32( strtabsize, &(ElfBase[0x74]) );
fillInU32( strtabsize + 0x100, &(ElfBase[0x98]) );
Expand Down
5 changes: 3 additions & 2 deletions bld/nwlib/c/libwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "wlib.h"
#include "ar.h"
#include "convert.h"
#include "roundmac.h"

#include "clibext.h"

Expand Down Expand Up @@ -97,7 +98,7 @@ void LibWalk( libfile io, const char *name, void (*rtn)( arch_header *, libfile
MemFree( arch.name );
MemFree( arch.ffname );
}
Round2var( arch.size );
arch.size = __ROUND_UP_SIZE_EVEN( arch.size );
LibSeek( io, pos + arch.size, SEEK_SET );
}
MemFree( arch.fnametab );
Expand Down Expand Up @@ -139,7 +140,7 @@ void OMFLibWalk( libfile io, char *name, void (*rtn)( arch_header *arch, libfile
LibSeek( io, offset, SEEK_SET );
rtn( &arch, io );
offset = LibTell( io );
offset = Round( offset, pagelen );
offset = __ROUND_UP_SIZE( offset, pagelen );
LibSeek( io, offset, SEEK_SET );
}
}
7 changes: 4 additions & 3 deletions bld/nwlib/c/omfutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@


#include "wlib.h"
#include "roundmac.h"

#include "clibext.h"

Expand Down Expand Up @@ -135,7 +136,7 @@ static bool InsertOmfDict( OmfLibBlock *lib_block, unsigned num_blocks, char *sy
omflib_hash( sym, len, &h, num_blocks );

/* + length byte */
entry_len = Round2( len + 1 ) + 2;
entry_len = __ROUND_UP_SIZE_EVEN( len + 1 ) + 2;
for( i = 0; i < num_blocks; i++ ) {
loc = lib_block[h.block].fflag * 2;
for( j = 0; j < NUM_BUCKETS; j++ ) {
Expand Down Expand Up @@ -268,12 +269,12 @@ void WriteOmfFile( sym_file *sfile )
* be word aligned
* + '!' character and length byte
*/
charCount += Round2( strlen( fname ) + 1 + 1 );
charCount += __ROUND_UP_SIZE_EVEN( strlen( fname ) + 1 + 1 );
WriteFileBody( sfile );
PadOmf( false );
for( sym = sfile->first; sym != NULL; sym = sym->next ) {
++symCount;
/* + length byte and word align */
charCount += Round2( sym->len + 1 );
charCount += __ROUND_UP_SIZE_EVEN( sym->len + 1 );
}
}
23 changes: 12 additions & 11 deletions bld/nwlib/c/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Open Watcom Project
*
* Copyright (c) 2002-2022 The Open Watcom Contributors. All Rights Reserved.
* Copyright (c) 2002-2023 The Open Watcom Contributors. All Rights Reserved.
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
Expand Down Expand Up @@ -33,6 +33,7 @@
#include "wlib.h"
#include "ar.h"
#include "convert.h"
#include "roundmac.h"

#include "clibext.h"

Expand Down Expand Up @@ -415,7 +416,7 @@ static unsigned_16 OptimalPageSize( void )
for( sfile = FileTable.first; sfile != NULL; sfile = sfile->next ) {
if( offset / page_size > (unsigned long)USHRT_MAX )
break;
offset += Round( sfile->arch.size, page_size );
offset += __ROUND_UP_SIZE( sfile->arch.size, page_size );
}
if( sfile == NULL ) {
break;
Expand Down Expand Up @@ -469,7 +470,7 @@ static void WriteArMlibFileTable( void )

switch( Options.libtype ) {
case WL_LTYPE_AR:
dict1_size = ( NumSymbols + 1 ) * sizeof(unsigned_32) + Round2( TotalSymbolLength );
dict1_size = ( NumSymbols + 1 ) * sizeof(unsigned_32) + __ROUND_UP_SIZE_EVEN( TotalSymbolLength );

header_size = AR_IDENT_LEN + AR_HEADER_SIZE + dict1_size;

Expand All @@ -484,7 +485,7 @@ static void WriteArMlibFileTable( void )
dict2_size = 0;

if( TotalNameLength > 0 ) {
header_size += AR_HEADER_SIZE + Round2( TotalNameLength );
header_size += AR_HEADER_SIZE + __ROUND_UP_SIZE_EVEN( TotalNameLength );
}

padding_string = "\0";
Expand All @@ -493,12 +494,12 @@ static void WriteArMlibFileTable( void )
default:
dict2_size = ( NumFiles + 1 ) * sizeof( unsigned_32 )
+ sizeof( unsigned_32 ) + NumSymbols * sizeof( unsigned_16 )
+ Round2( TotalSymbolLength );
+ __ROUND_UP_SIZE_EVEN( TotalSymbolLength );

header_size += AR_HEADER_SIZE + dict2_size;

if( TotalNameLength > 0 ) {
header_size += AR_HEADER_SIZE + Round2( TotalNameLength );
header_size += AR_HEADER_SIZE + __ROUND_UP_SIZE_EVEN( TotalNameLength );
}

padding_string = AR_FILE_PADDING_STRING;
Expand All @@ -513,9 +514,9 @@ static void WriteArMlibFileTable( void )
+ TotalSymbolLength;

header_size = LIBMAG_LEN + LIB_CLASS_LEN + LIB_DATA_LEN
+ LIB_HEADER_SIZE + Round2( dict2_size )
+ LIB_HEADER_SIZE + Round2( TotalNameLength )
+ LIB_HEADER_SIZE + Round2( TotalFFNameLength );
+ LIB_HEADER_SIZE + __ROUND_UP_SIZE_EVEN( dict2_size )
+ LIB_HEADER_SIZE + __ROUND_UP_SIZE_EVEN( TotalNameLength )
+ LIB_HEADER_SIZE + __ROUND_UP_SIZE_EVEN( TotalFFNameLength );

padding_string = LIB_FILE_PADDING_STRING;
padding_string_len = LIB_FILE_PADDING_STRING_LEN;
Expand All @@ -531,9 +532,9 @@ static void WriteArMlibFileTable( void )
sfile->u.new_offset = obj_offset + header_size;
sfile->index = ++index;
if( isBSD && ( sfile->name_length > AR_NAME_LEN || strchr( sfile->arch.name, ' ' ) != NULL ) ) {
obj_offset += Round2( sfile->arch.size + sfile->name_length ) + AR_HEADER_SIZE;
obj_offset += __ROUND_UP_SIZE_EVEN( sfile->arch.size + sfile->name_length ) + AR_HEADER_SIZE;
} else {
obj_offset += Round2( sfile->arch.size ) + AR_HEADER_SIZE;
obj_offset += __ROUND_UP_SIZE_EVEN( sfile->arch.size ) + AR_HEADER_SIZE;
}
}

Expand Down
6 changes: 3 additions & 3 deletions bld/nwlib/c/wlibutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "wlib.h"
#include "wio.h"
#include "pathgrp2.h"
#include "roundmac.h"

#include "clibext.h"

Expand All @@ -57,12 +58,11 @@ void GetFileContents( const char *name, libfile io, arch_header *arch, char **co
{
size_t size;

size = arch->size;
if( size == 0 ) {
if( arch->size == 0 ) {
*contents = NULL;
return;
}
Round2var( size );
size = __ROUND_UP_SIZE_EVEN( arch->size );
*contents = MemAlloc( size );
if( LibRead( io, *contents, size ) != size ) {
BadLibrary( name );
Expand Down
5 changes: 1 addition & 4 deletions bld/nwlib/h/wlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Open Watcom Project
*
* Copyright (c) 2002-2020 The Open Watcom Contributors. All Rights Reserved.
* Copyright (c) 2002-2023 The Open Watcom Contributors. All Rights Reserved.
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
Expand Down Expand Up @@ -73,9 +73,6 @@
#include "exepe.h"
#include "exenov.h"

#define Round(x,s) (((x) + (s) - 1) & ~((s) - 1))
#define Round2(x) Round((x),2)
#define Round2var(x) if((x) & 1) ++(x)

#define FILE_TEMPLATE_MASK "00000000"
#define FILE_TEMPLATE_FMT "%8.8d"
Loading

0 comments on commit 0567a26

Please sign in to comment.