Skip to content

Commit

Permalink
nwlib: use const pointer to arch_header where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalak committed Jun 4, 2024
1 parent 8f97687 commit 63fb699
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 74 deletions.
2 changes: 1 addition & 1 deletion bld/nwlib/c/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static void PutARValueMode( char *element, uint_32 value, ar_len desired_len )
}
}

void CreateARHeader( ar_header *ar, arch_header * arch )
void CreateARHeader( ar_header *ar, const arch_header *arch )
{
PutARName( ar->name, arch->name );
PutARValue( ar->date, arch->date, AR_DATE_LEN );
Expand Down
38 changes: 20 additions & 18 deletions bld/nwlib/c/implib.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static orl_sec_handle FindSec( obj_file *ofile, const char *name )
return( found_sec_handle );
}

static bool elfAddImport( libfile io, long header_offset, arch_header *arch )
static bool elfAddImport( libfile io, long header_offset, const arch_header *arch )
{
obj_file *ofile;
orl_sec_handle sym_sec;
Expand All @@ -143,6 +143,7 @@ static bool elfAddImport( libfile io, long header_offset, arch_header *arch )
name_len dllName;
Elf32_Word ElfMagic;
char *dll_name;
arch_header tmp_arch;

LibSeek( io, header_offset, SEEK_SET );
if( LibRead( io, &ElfMagic, sizeof( ElfMagic ) ) != sizeof( ElfMagic ) ) {
Expand Down Expand Up @@ -180,21 +181,20 @@ static bool elfAddImport( libfile io, long header_offset, arch_header *arch )
string_sec = ORLSecGetStringTable( sym_sec );
ORLSecGetContents( string_sec, (unsigned_8 **)&strings );

arch->ffname = arch->name;
dllName.name = arch->name = dll_name = MemDupStr( TrimPath( arch->name ) );
tmp_arch.ffname = arch->name;
dllName.name = tmp_arch.name = dll_name = MemDupStr( TrimPath( arch->name ) );
dllName.len = strlen( dllName.name );

ElfMKImport( arch, ELF, export_size, &dllName, strings, export_table, sym_table, processor );
ElfMKImport( &tmp_arch, ELF, export_size, &dllName, strings, export_table, sym_table, processor );

MemFree( dll_name );
arch->name = arch->ffname;

CloseORLObjFile( ofile );
MemFree( ofile );
return( true );
}

static void importOs2Table( libfile io, arch_header *arch, name_len *dllName, bool coff_obj, importType type, unsigned length )
static void importOs2Table( libfile io, const arch_header *arch, name_len *dllName, bool coff_obj, importType type, unsigned length )
{
unsigned_16 ordinal;
char sym_name[256]; /* maximum name len is 255 characters */
Expand Down Expand Up @@ -229,7 +229,7 @@ static void importOs2Table( libfile io, arch_header *arch, name_len *dllName, bo
}
}

static void os2AddImport( libfile io, long header_offset, arch_header *arch )
static void os2AddImport( libfile io, long header_offset, const arch_header *arch )
{
os2_exe_header os2_header;
char dll_name[256]; /* maximum name len is 255 characters */
Expand Down Expand Up @@ -270,7 +270,7 @@ static void AddSym2( name_len *n1, const char *n2, symbol_strength strength, uns
MemFree( sym_name );
}

static void coffAddImportOverhead( arch_header *arch, name_len *dllName, processor_type processor )
static void coffAddImportOverhead( const arch_header *arch, name_len *dllName, processor_type processor )
{
char *buffer;
char *mod_name;
Expand All @@ -290,7 +290,7 @@ static void coffAddImportOverhead( arch_header *arch, name_len *dllName, process
MemFree( mod_name );
}

static void os2FlatAddImport( libfile io, long header_offset, arch_header *arch )
static void os2FlatAddImport( libfile io, long header_offset, const arch_header *arch )
{
os2_flat_header os2_header;
char dll_name[256]; /* maximum name len is 255 characters */
Expand All @@ -317,7 +317,7 @@ static void os2FlatAddImport( libfile io, long header_offset, arch_header *arch
}
}

static bool nlmAddImport( libfile io, long header_offset, arch_header *arch )
static bool nlmAddImport( libfile io, long header_offset, const arch_header *arch )
{
nlm_header nlm;
char dll_name[256]; /* maximum name len is 255 characters */
Expand Down Expand Up @@ -347,7 +347,7 @@ static bool nlmAddImport( libfile io, long header_offset, arch_header *arch )
return( true );
}

static void peAddImport( libfile io, long header_offset, arch_header *arch )
static void peAddImport( libfile io, long header_offset, const arch_header *arch )
{
obj_file *ofile;
orl_sec_handle export_sec;
Expand All @@ -364,6 +364,7 @@ static void peAddImport( libfile io, long header_offset, arch_header *arch )
importType type;
bool coff_obj;
long adjust;
arch_header tmp_arch;

LibSeek( io, header_offset, SEEK_SET );
if( Options.libtype == WL_LTYPE_MLIB ) {
Expand Down Expand Up @@ -420,22 +421,23 @@ static void peAddImport( libfile io, long header_offset, arch_header *arch )
ord_table = (Coff32_EOrd *)(edata + export_header->OrdTableRVA - export_base.u._32[I64LO32] + adjust);
ordinal_base = export_header->ordBase;

arch->name = edata + export_header->nameRVA - export_base.u._32[I64LO32] + adjust;
arch->ffname = NULL;
dllName.name = arch->name;
tmp_arch = *arch;
tmp_arch.name = edata + export_header->nameRVA - export_base.u._32[I64LO32] + adjust;
tmp_arch.ffname = NULL;
dllName.name = tmp_arch.name;
dllName.len = strlen( dllName.name );

if( coff_obj ) {
coffAddImportOverhead( arch, &dllName, processor );
coffAddImportOverhead( &tmp_arch, &dllName, processor );
}
for( i = 0; i < export_header->numNamePointer; i++ ) {
sym_name = &(edata[name_table[i] - export_base.u._32[I64LO32] + adjust]);
if( coff_obj ) {
CoffMKImport( arch, ORDINAL, ord_table[i] + ordinal_base, &dllName, sym_name, NULL, processor );
CoffMKImport( &tmp_arch, ORDINAL, ord_table[i] + ordinal_base, &dllName, sym_name, NULL, processor );
AddSym2( &str_coff_imp_prefix, sym_name, SYM_WEAK, 0 );
} else {
type = Options.r_ordinal ? ORDINAL : NAMED;
OmfMKImport( arch, type, ord_table[i] + ordinal_base, &dllName, sym_name, NULL, WL_PROC_X86 );
OmfMKImport( &tmp_arch, type, ord_table[i] + ordinal_base, &dllName, sym_name, NULL, WL_PROC_X86 );
// AddSym2( &str_coff_imp_prefix, sym_name, SYM_WEAK, 0 );
}
if( processor == WL_PROC_PPC ) {
Expand Down Expand Up @@ -1049,7 +1051,7 @@ void ElfWriteImport( libfile io, sym_file *sfile )
}
}

bool AddImport( libfile io, arch_header *arch )
bool AddImport( libfile io, const arch_header *arch )
{
unsigned_32 ne_header_off;
long header_offset;
Expand Down
53 changes: 29 additions & 24 deletions bld/nwlib/c/libwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
#include "clibext.h"


static void AllocFNameTab( libfile io, arch_header *arch, arch_dict *dict )
/*************************************************************************/
static void AllocFNameTab( libfile io, const arch_header *arch, arch_dict *dict )
/*******************************************************************************/
{
MemFree( dict->fnametab );
GetFileContents( io, arch, &dict->fnametab );
}

static void AllocFFNameTab( libfile io, arch_header *arch, arch_dict *dict )
/**************************************************************************/
static void AllocFFNameTab( libfile io, const arch_header *arch, arch_dict *dict )
/********************************************************************************/
{
MemFree( dict->ffnametab );
GetFileContents( io, arch, &dict->ffnametab );
Expand All @@ -56,14 +56,14 @@ static void AllocFFNameTab( libfile io, arch_header *arch, arch_dict *dict )
}


void LibWalk( libfile io, arch_header *arch, libwalk_fn *rtn )
/************************************************************/
void LibWalk( libfile io, const arch_header *arch, libwalk_fn *rtn )
/******************************************************************/
{
long pos;
char *oldname;
arch_header tmp_arch;

oldname = arch->name;
if( arch->libtype == WL_LTYPE_OMF ) {
tmp_arch.libtype = arch->libtype;
if( tmp_arch.libtype == WL_LTYPE_OMF ) {
unsigned_16 pagelen;
char buff[MAX_IMPORT_STRING];
unsigned_8 rec_type;
Expand All @@ -82,6 +82,13 @@ void LibWalk( libfile io, arch_header *arch, libwalk_fn *rtn )
}
LibSeek( io, pos, SEEK_CUR );
pos = LibTell( io );
tmp_arch.date = arch->date;
tmp_arch.uid = arch->uid;
tmp_arch.gid = arch->gid;
tmp_arch.mode = arch->mode;
tmp_arch.size = arch->size;
tmp_arch.ffname = NULL;
tmp_arch.name = buff;
while( LibRead( io, &rec_type, sizeof( rec_type ) ) == sizeof( rec_type ) && ( rec_type == CMD_THEADR ) ) {
if( LibRead( io, &rec_len, sizeof( rec_len ) ) != sizeof( rec_len ) )
BadLibrary( io );
Expand All @@ -90,9 +97,8 @@ void LibWalk( libfile io, arch_header *arch, libwalk_fn *rtn )
if( LibRead( io, buff, str_len ) != str_len )
BadLibrary( io );
buff[str_len] = '\0';
arch->name = buff;
LibSeek( io, pos, SEEK_SET );
rtn( io, arch );
rtn( io, &tmp_arch );
pos = LibTell( io );
pos = __ROUND_UP_SIZE( pos, pagelen );
LibSeek( io, pos, SEEK_SET );
Expand All @@ -113,7 +119,7 @@ void LibWalk( libfile io, arch_header *arch, libwalk_fn *rtn )
if( strncmp( ar.header_ident, AR_HEADER_IDENT, AR_HEADER_IDENT_LEN ) ) {
BadLibrary( io );
}
GetARHeaderValues( &ar, arch );
GetARHeaderValues( &ar, &tmp_arch );
pos = LibTell( io );
if( ar.name[0] == '/'
&& ar.name[1] == ' '
Expand All @@ -122,32 +128,31 @@ void LibWalk( libfile io, arch_header *arch, libwalk_fn *rtn )
/*
dict_count++;
if( dict_count == 2 ) {
error = readDict( arch );
error = readDict( &tmp_arch );
} else {
error = MoveAheadFrom( arch );
updateNewArchive( arch );
error = MoveAheadFrom( &tmp_arch );
updateNewArchive( &tmp_arch );
}
*/
} else if( ar.name[0] == '/'
&& ar.name[1] == '/'
&& ar.name[2] == ' ' ) {
AllocFNameTab( io, arch, &dict );
AllocFNameTab( io, &tmp_arch, &dict );
} else if( ar.name[0] == '/'
&& ar.name[1] == '/'
&& ar.name[2] == '/' ) {
AllocFFNameTab( io, arch, &dict );
AllocFFNameTab( io, &tmp_arch, &dict );
} else {
arch->name = GetARName( io, &ar, &dict );
arch->ffname = GetFFName( &dict );
rtn( io, arch );
MemFree( arch->name );
MemFree( arch->ffname );
tmp_arch.name = GetARName( io, &ar, &dict );
tmp_arch.ffname = GetFFName( &dict );
rtn( io, &tmp_arch );
MemFree( tmp_arch.name );
MemFree( tmp_arch.ffname );
}
pos += __ROUND_UP_SIZE_EVEN( arch->size );
pos += __ROUND_UP_SIZE_EVEN( tmp_arch.size );
LibSeek( io, pos, SEEK_SET );
}
MemFree( dict.fnametab );
MemFree( dict.ffnametab );
}
arch->name = oldname;
}
12 changes: 6 additions & 6 deletions bld/nwlib/c/proclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
#include "clibext.h"


static void SkipObject( libfile io, arch_header *arch )
static void SkipObject( libfile io, const arch_header *arch )
{
if( arch->libtype == WL_LTYPE_OMF ) {
OmfSkipObject( io );
}
}

static void CopyObj( libfile src, libfile dst, arch_header *arch )
static void CopyObj( libfile src, libfile dst, const arch_header *arch )
{
if( arch->libtype == WL_LTYPE_OMF ) {
OmfExtract( src, dst );
Expand All @@ -53,7 +53,7 @@ static void CopyObj( libfile src, libfile dst, arch_header *arch )
}
}

static void ExtractObj( libfile src, const char *name, arch_header *arch, const char *newname )
static void ExtractObj( libfile src, const char *name, const arch_header *arch, const char *newname )
{
long pos;
libfile dst;
Expand All @@ -71,7 +71,7 @@ static void ExtractObj( libfile src, const char *name, arch_header *arch, const
}
}

static void ProcessOneObject( libfile io, arch_header *arch )
static void ProcessOneObject( libfile io, const arch_header *arch )
{
lib_cmd *cmd;
bool deleted;
Expand Down Expand Up @@ -115,12 +115,12 @@ static void ProcessOneObject( libfile io, arch_header *arch )
}
}

static void AddOneObject( libfile io, arch_header *arch )
static void AddOneObject( libfile io, const arch_header *arch )
{
AddObjectSymbols( io, LibTell( io ), arch );
}

static void DelOneObject( libfile io, arch_header *arch )
static void DelOneObject( libfile io, const arch_header *arch )
{
RemoveObjectSymbols( arch );
SkipObject( io, arch );
Expand Down
22 changes: 11 additions & 11 deletions bld/nwlib/c/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ static void RemoveFromHashTable( sym_entry *sym )
}


static sym_file *NewSymFile( arch_header *arch, file_type obj_type )
/******************************************************************/
static sym_file *NewSymFile( const arch_header *arch, file_type obj_type )
/************************************************************************/
{
sym_file *sfile;

Expand Down Expand Up @@ -251,8 +251,8 @@ static int CompSyms( const void *ap, const void *bp )
return( strcmp( a->name, b->name ) );
}

static void WriteFileHeader( libfile io, arch_header *arch )
/**********************************************************/
static void WriteFileHeader( libfile io, const arch_header *arch )
/****************************************************************/
{
ar_header ar;

Expand Down Expand Up @@ -943,8 +943,8 @@ void DumpHashTable( void )
#endif /* DEVBUILD */


bool RemoveObjectSymbols( arch_header *arch )
/*******************************************/
bool RemoveObjectSymbols( const arch_header *arch )
/*************************************************/
{
sym_file *sfile;
sym_file *sfile_prev;
Expand Down Expand Up @@ -983,8 +983,8 @@ bool RemoveObjectSymbols( arch_header *arch )
return( false );
}

void AddObjectSymbols( libfile io, long offset, arch_header *arch )
/*****************************************************************/
void AddObjectSymbols( libfile io, long offset, const arch_header *arch )
/***********************************************************************/
{
obj_file *ofile;
file_type obj_type;
Expand Down Expand Up @@ -1022,7 +1022,7 @@ void AddObjectSymbols( libfile io, long offset, arch_header *arch )
MemFree( ofile );
}

void OmfMKImport( arch_header *arch, importType type,
void OmfMKImport( const arch_header *arch, importType type,
long ordinal, name_len *dllName, const char *symName,
const char *exportedName, processor_type processor )
{
Expand All @@ -1046,7 +1046,7 @@ void OmfMKImport( arch_header *arch, importType type,
AddSym( symName, SYM_STRONG, 0 );
}

void CoffMKImport( arch_header *arch, importType type,
void CoffMKImport( const arch_header *arch, importType type,
long ordinal, name_len *dllName, const char *symName,
const char *exportedName, processor_type processor )
{
Expand Down Expand Up @@ -1079,7 +1079,7 @@ void CoffMKImport( arch_header *arch, importType type,
}
}

void ElfMKImport( arch_header *arch, importType type, long export_size,
void ElfMKImport( const arch_header *arch, importType type, long export_size,
name_len *dllName, const char *strings, Elf32_Export *export_table,
Elf32_Sym *sym_table, processor_type processor )
{
Expand Down
2 changes: 1 addition & 1 deletion bld/nwlib/c/wlibutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int SymbolNameCmp( const char *s1, const char *s2)
}
}

void GetFileContents( libfile io, arch_header *arch, char **contents )
void GetFileContents( libfile io, const arch_header *arch, char **contents )
{
size_t size;

Expand Down
Loading

0 comments on commit 63fb699

Please sign in to comment.