clang-format more files.

"Gofmt's style is nobody's favourite, but gofmt is everybody's favourite."
    - Rob Pike
This commit is contained in:
Markus F.X.J. Oberhumer
2021-01-04 20:26:31 +01:00
parent 8236276a90
commit 2575eef3c0
14 changed files with 1590 additions and 1871 deletions
+98 -156
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com>
*/
#include "conf.h"
#include "file.h"
@@ -34,75 +33,55 @@
#include "p_tos.h"
#include "linker.h"
static const
static const CLANG_FORMAT_DUMMY_STATEMENT
#include "stub/m68k-atari.tos.h"
//#define TESTING 1
/*************************************************************************
//
**************************************************************************/
#define FH_SIZE sizeof(tos_header_t)
#define FH_SIZE sizeof(tos_header_t)
PackTos::PackTos(InputFile *f) :
super(f)
{
PackTos::PackTos(InputFile *f) : super(f) {
bele = &N_BELE_RTP::be_policy;
COMPILE_TIME_ASSERT(FH_SIZE == 28);
COMPILE_TIME_ASSERT_ALIGNED1(tos_header_t)
}
const int *PackTos::getCompressionMethods(int method, int level) const
{
bool small = ih.fh_text + ih.fh_data <= 256*1024;
const int *PackTos::getCompressionMethods(int method, int level) const {
bool small = ih.fh_text + ih.fh_data <= 256 * 1024;
return Packer::getDefaultCompressionMethods_8(method, level, small);
}
const int *PackTos::getFilters() const { return nullptr; }
const int *PackTos::getFilters() const
{
return nullptr;
}
Linker *PackTos::newLinker() const { return new ElfLinkerM68k; }
Linker* PackTos::newLinker() const
{
return new ElfLinkerM68k;
}
void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra)
{
void PackTos::LinkerSymbols::LoopInfo::init(unsigned count_, bool allow_dbra) {
count = value = count_;
if (count == 0)
mode = LOOP_NONE;
else if (count <= 65536 && allow_dbra)
{
else if (count <= 65536 && allow_dbra) {
mode = LOOP_DBRA;
value -= 1;
value &= 0xffff;
}
else if (count <= 65536)
{
} else if (count <= 65536) {
mode = LOOP_SUBQ_W;
value &= 0xffff;
}
else
} else
mode = LOOP_SUBQ_L;
}
unsigned PackTos::getDecomprOffset(int method, int small) const
{
unsigned PackTos::getDecomprOffset(int method, int small) const {
UNUSED(small);
if (M_IS_NRV2B(method))
return 2; // FIXME: do not hardcode this value
return 2; // FIXME: do not hardcode this value
else if (M_IS_NRV2D(method))
return 2; // FIXME: do not hardcode this value
return 2; // FIXME: do not hardcode this value
else if (M_IS_NRV2E(method))
return 2; // FIXME: do not hardcode this value
return 2; // FIXME: do not hardcode this value
else if (M_IS_LZMA(method))
return linker->getSectionSize("__mulsi3");
else
@@ -110,13 +89,11 @@ unsigned PackTos::getDecomprOffset(int method, int small) const
return 0;
}
void PackTos::buildLoader(const Filter *ft)
{
void PackTos::buildLoader(const Filter *ft) {
assert(ft->id == 0);
initLoader(stub_m68k_atari_tos, sizeof(stub_m68k_atari_tos));
//linker->dumpSymbols();
// linker->dumpSymbols();
//
// part 1a
@@ -132,8 +109,7 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("set_up21_d4.l");
assert(symbols.loop1.count || symbols.loop2.count);
if (symbols.loop1.count)
{
if (symbols.loop1.count) {
if (symbols.loop1.value <= 127)
addLoader("loop1_set_count.b");
else if (symbols.loop1.value <= 65535)
@@ -151,8 +127,7 @@ void PackTos::buildLoader(const Filter *ft)
else
throwBadLoader();
}
if (symbols.loop2.count)
{
if (symbols.loop2.count) {
assert(symbols.loop2.mode == symbols.LOOP_DBRA);
addLoader(opt->small ? "loop2.small" : "loop2.fast");
}
@@ -222,8 +197,7 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("__mulsi3");
addLoader(opt->small ? "lzma.small" : "lzma.fast");
addLoader("lzma.finish");
}
else
} else
throwBadLoader();
if (symbols.need_reloc)
@@ -240,41 +214,40 @@ void PackTos::buildLoader(const Filter *ft)
addLoader("jmp_stack");
}
/*************************************************************************
//
**************************************************************************/
/* flags for curproc->memflags */
/* also used for program headers fh_flag */
#define F_FASTLOAD 0x01 // don't zero heap
#define F_ALTLOAD 0x02 // OK to load in alternate ram
#define F_ALTALLOC 0x04 // OK to malloc from alt. ram
#define F_SMALLTPA 0x08 // used in MagiC: TPA can be allocated
// as specified in the program header
// rather than the biggest free memory
// block
#define F_MEMFLAGS 0xf0 // reserved for future use
#define F_SHTEXT 0x800 // program's text may be shared
#define F_FASTLOAD 0x01 // don't zero heap
#define F_ALTLOAD 0x02 // OK to load in alternate ram
#define F_ALTALLOC 0x04 // OK to malloc from alt. ram
#define F_SMALLTPA \
0x08 // used in MagiC: TPA can be allocated
// as specified in the program header
// rather than the biggest free memory
// block
#define F_MEMFLAGS 0xf0 // reserved for future use
#define F_SHTEXT 0x800 // program's text may be shared
#define F_MINALT 0xf0000000 // used to decide which type of RAM to load in
#define F_MINALT 0xf0000000 // used to decide which type of RAM to load in
#define F_ALLOCZERO 0x2000 // zero mem, for bugged (GEM...) programs
#define F_ALLOCZERO 0x2000 // zero mem, for bugged (GEM...) programs
/* Bit in Mxalloc's arg for "don't auto-free this memory" */
#define F_KEEP 0x4000
#define F_KEEP 0x4000
#define F_OS_SPECIAL 0x8000 // mark as a special process
#define F_OS_SPECIAL 0x8000 // mark as a special process
/* flags for curproc->memflags (that is, fh_flag) and also Mxalloc mode. */
/* (Actually, when users call Mxalloc, they add 0x10 to what you see here) */
#define F_PROTMODE 0xf0 // protection mode bits
#define F_PROT_P 0x00 // no read or write
#define F_PROT_G 0x10 // any access OK
#define F_PROT_S 0x20 // any super access OK
#define F_PROT_PR 0x30 // any read OK, no write
#define F_PROT_I 0x40 // invalid page
#define F_PROTMODE 0xf0 // protection mode bits
#define F_PROT_P 0x00 // no read or write
#define F_PROT_G 0x10 // any access OK
#define F_PROT_S 0x20 // any super access OK
#define F_PROT_PR 0x30 // any read OK, no write
#define F_PROT_I 0x40 // invalid page
/*************************************************************************
// util
@@ -282,9 +255,8 @@ void PackTos::buildLoader(const Filter *ft)
// checkFileHeader() checks ih for legal but unsupported values
**************************************************************************/
int PackTos::readFileHeader()
{
fi->seek(0,SEEK_SET);
int PackTos::readFileHeader() {
fi->seek(0, SEEK_SET);
fi->readx(&ih, FH_SIZE);
if (ih.fh_magic != 0x601a)
return 0;
@@ -293,31 +265,27 @@ int PackTos::readFileHeader()
return UPX_F_ATARI_TOS;
}
bool PackTos::checkFileHeader()
{
bool PackTos::checkFileHeader() {
const unsigned f = ih.fh_flag;
//printf("flags: 0x%x, text: %d, data: %d, bss: %d, sym: %d\n", f, (int)ih.fh_text, (int)ih.fh_data, (int)ih.fh_bss, (int)ih.fh_sym);
// printf("flags: 0x%x, text: %d, data: %d, bss: %d, sym: %d\n", f, (int) ih.fh_text,
// (int) ih.fh_data, (int) ih.fh_bss, (int) ih.fh_sym);
if ((ih.fh_text & 1) || (ih.fh_data & 1))
throwCantPack("odd size values in text/data");
if (f & F_OS_SPECIAL)
throwCantPack("I won't pack F_OS_SPECIAL programs");
if ((f & F_PROTMODE) > F_PROT_I)
throwCantPack("invalid protection mode");
if ((f & F_PROTMODE) != F_PROT_P)
{
if ((f & F_PROTMODE) != F_PROT_P) {
if (opt->force < 1)
throwCantPack("no private memory protection; use option '-f' to force packing");
}
if (f & F_SHTEXT)
{
if (f & F_SHTEXT) {
if (opt->force < 1)
throwCantPack("shared text segment; use option '-f' to force packing");
}
#if 0
// fh_reserved seems to be unused
if (ih.fh_reserved != 0)
{
if (ih.fh_reserved != 0) {
if (opt->force < 1)
throwCantPack("reserved header field set; use option '-f' to force packing");
}
@@ -325,15 +293,13 @@ bool PackTos::checkFileHeader()
return true;
}
/*************************************************************************
// relocs
**************************************************************************/
// Check relocation for errors to make sure our loader can handle it.
static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
unsigned *nrelocs, unsigned *relocsize, unsigned *overlay)
{
static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize, unsigned *nrelocs,
unsigned *relocsize, unsigned *overlay) {
unsigned fixup = get_be32(relocs);
unsigned last_fixup = fixup;
unsigned i = 4;
@@ -342,25 +308,24 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
assert(fixup > 0);
*nrelocs = 1;
for (;;)
{
if (fixup & 1) // must be word-aligned
for (;;) {
if (fixup & 1) // must be word-aligned
return -1;
if (fixup + 4 > isize) // too far
if (fixup + 4 > isize) // too far
return -1;
if (i >= rsize) // premature EOF in relocs
if (i >= rsize) // premature EOF in relocs
return -1;
unsigned c = relocs[i++];
if (c == 0) // end marker
if (c == 0) // end marker
break;
else if (c == 1) // increase fixup, no reloc
else if (c == 1) // increase fixup, no reloc
fixup += 254;
else if (c & 1) // must be word-aligned
else if (c & 1) // must be word-aligned
return -1;
else // next reloc is here
else // next reloc is here
{
fixup += c;
if (fixup - last_fixup < 4) // overlapping relocation
if (fixup - last_fixup < 4) // overlapping relocation
return -1;
last_fixup = fixup;
*nrelocs += 1;
@@ -372,13 +337,11 @@ static int check_relocs(const upx_byte *relocs, unsigned rsize, unsigned isize,
return 0;
}
/*************************************************************************
//
**************************************************************************/
bool PackTos::canPack()
{
bool PackTos::canPack() {
if (!readFileHeader())
return false;
@@ -394,22 +357,18 @@ bool PackTos::canPack()
return true;
}
void PackTos::fileInfo()
{
void PackTos::fileInfo() {
if (!readFileHeader())
return;
con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n",
(int)ih.fh_text, (int)ih.fh_data, (int)ih.fh_sym, (int)ih.fh_bss, (int)ih.fh_flag);
con_fprintf(stdout, " text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n", (int) ih.fh_text,
(int) ih.fh_data, (int) ih.fh_sym, (int) ih.fh_bss, (int) ih.fh_flag);
}
/*************************************************************************
//
**************************************************************************/
void PackTos::pack(OutputFile *fo)
{
void PackTos::pack(OutputFile *fo) {
unsigned t;
unsigned nrelocs = 0;
unsigned relocsize = 0;
@@ -437,14 +396,14 @@ void PackTos::pack(OutputFile *fo)
fi->seek(FH_SIZE, SEEK_SET);
// read text + data
t = i_text + i_data;
fi->readx(ibuf,t);
fi->readx(ibuf, t);
// skip symbols
if (i_sym && opt->exact)
throwCantPackExact();
fi->seek(i_sym,SEEK_CUR);
fi->seek(i_sym, SEEK_CUR);
// read relocations + overlay
overlay = file_size - (FH_SIZE + i_text + i_data + i_sym);
fi->readx(ibuf+t,overlay);
fi->readx(ibuf + t, overlay);
#if 0 || (TESTING)
printf("text: %d, data: %d, sym: %d, bss: %d, flags=0x%x\n",
@@ -454,23 +413,19 @@ void PackTos::pack(OutputFile *fo)
// Check relocs (see load_and_reloc() in freemint/sys/memory.c).
// Must work around TOS bugs and lots of broken programs.
if (overlay < 4)
{
if (overlay < 4) {
// Bug workaround: Whatever this is, silently keep it in
// the (unused) relocations for byte-identical unpacking.
relocsize = overlay;
overlay = 0;
}
else if (get_be32(ibuf+t) == 0)
{
} else if (get_be32(ibuf + t) == 0) {
// Bug workaround - check the empty fixup before testing fh_reloc.
relocsize = 4;
overlay -= 4;
}
else if (ih.fh_reloc != 0)
} else if (ih.fh_reloc != 0)
relocsize = 0;
else {
int r = check_relocs(ibuf+t, overlay, t, &nrelocs, &relocsize, &overlay);
int r = check_relocs(ibuf + t, overlay, t, &nrelocs, &relocsize, &overlay);
if (r != 0)
throwCantPack("bad relocation table");
symbols.need_reloc = true;
@@ -484,8 +439,8 @@ void PackTos::pack(OutputFile *fo)
// Append original fileheader.
t += relocsize;
ih.fh_sym = 0; // we stripped all symbols
memcpy(ibuf+t, &ih, FH_SIZE);
ih.fh_sym = 0; // we stripped all symbols
memcpy(ibuf + t, &ih, FH_SIZE);
t += FH_SIZE;
#if 0 || (TESTING)
printf("xx3 reloc: %d, overlay: %d, t: %d\n", relocsize, overlay, t);
@@ -505,7 +460,8 @@ void PackTos::pack(OutputFile *fo)
// prepare filter
Filter ft(ph.level);
// compress (max_match = 65535)
upx_compress_config_t cconf; cconf.reset();
upx_compress_config_t cconf;
cconf.reset();
cconf.conf_ucl.max_match = 65535;
cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack
compressWithFilters(&ft, 512, &cconf);
@@ -522,8 +478,7 @@ void PackTos::pack(OutputFile *fo)
unsigned o_text, o_data, o_bss;
unsigned e_len, d_len, d_off;
for (;;)
{
for (;;) {
// The decompressed data will now get placed at this offset:
unsigned offset = (ph.u_len + ph.overlap_overhead) - ph.c_len;
@@ -539,8 +494,7 @@ void PackTos::pack(OutputFile *fo)
o_bss = i_bss;
// word align len of compressed data
while (o_data & 1)
{
while (o_data & 1) {
obuf[o_data++] = 0;
offset++;
}
@@ -551,8 +505,7 @@ void PackTos::pack(OutputFile *fo)
o_data += d_len;
// dword align the len of the final data segment
while (o_data & 3)
{
while (o_data & 3) {
obuf[o_data++] = 0;
offset++;
}
@@ -566,7 +519,7 @@ void PackTos::pack(OutputFile *fo)
// dirty bss
unsigned dirty_bss = (o_data + offset) - (i_text + i_data);
//printf("real dirty_bss: %d\n", dirty_bss);
// printf("real dirty_bss: %d\n", dirty_bss);
// dword align (or 16 - for speedup when clearing the dirty bss)
const unsigned dirty_bss_align = opt->small ? 4 : 16;
while (dirty_bss & (dirty_bss_align - 1))
@@ -581,13 +534,10 @@ void PackTos::pack(OutputFile *fo)
o_bss++;
// update symbols for buildLoader()
if (opt->small)
{
if (opt->small) {
symbols.loop1.init(o_data / 4);
symbols.loop2.init(0);
}
else
{
} else {
symbols.loop1.init(o_data / 160);
symbols.loop2.init((o_data % 160) / 4);
}
@@ -597,8 +547,8 @@ void PackTos::pack(OutputFile *fo)
symbols.up31_base_d4 = d_off + offset;
symbols.up21_a6 = symbols.up21_d4 - (i_text + i_data);
symbols.up31_base_a6 = symbols.up31_base_d4 - (i_text + i_data);
assert((int)symbols.up21_a6 > 0);
assert((int)symbols.up31_base_a6 > 0);
assert((int) symbols.up21_a6 > 0);
assert((int) symbols.up31_base_a6 > 0);
const unsigned c = linker->getSymbolOffset("code_on_stack");
unsigned d;
@@ -612,7 +562,7 @@ void PackTos::pack(OutputFile *fo)
// now re-build loader
buildLoader(&ft);
unsigned new_lsize = getLoaderSize();
//printf("buildLoader %d %d\n", new_lsize, initial_lsize);
// printf("buildLoader %d %d\n", new_lsize, initial_lsize);
assert(new_lsize <= initial_lsize);
if (new_lsize == last_lsize && memcmp(getLoader(), last_loader, last_lsize) == 0)
break;
@@ -667,13 +617,10 @@ void PackTos::pack(OutputFile *fo)
// set new file_hdr
memcpy(&oh, &ih, FH_SIZE);
if (opt->atari_tos.split_segments)
{
if (opt->atari_tos.split_segments) {
oh.fh_text = o_text;
oh.fh_data = o_data;
}
else
{
} else {
// put everything into the text segment
oh.fh_text = o_text + o_data;
oh.fh_data = 0;
@@ -701,11 +648,11 @@ void PackTos::pack(OutputFile *fo)
// write new file header, loader and compressed file
fo->write(&oh, FH_SIZE);
fo->write(loader, o_text); // entry
fo->write(loader, o_text); // entry
if (opt->debug.dump_stub_loader)
OutputFile::dump(opt->debug.dump_stub_loader, loader, o_text);
memcpy(obuf + d_off, getLoader() + e_len, d_len); // copy decompressor
fo->write(obuf, o_data); // compressed + decompressor
fo->write(obuf, o_data); // compressed + decompressor
// write empty relocation fixup
fo->write("\x00\x00\x00\x00", 4);
@@ -721,20 +668,18 @@ void PackTos::pack(OutputFile *fo)
throwNotCompressible();
}
/*************************************************************************
//
**************************************************************************/
int PackTos::canUnpack()
{
int PackTos::canUnpack() {
if (!readFileHeader())
return false;
if (!readPackHeader(768))
return false;
// check header as set by packer
if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0
|| ih.fh_sym != 0 || ih.fh_reserved != 0 || ih.fh_reloc > 1)
if ((ih.fh_text & 3) != 0 || (ih.fh_data & 3) != 0 || (ih.fh_bss & 3) != 0 || ih.fh_sym != 0 ||
ih.fh_reserved != 0 || ih.fh_reloc > 1)
throwCantUnpack("program header damaged");
// generic check
if (!checkFileHeader())
@@ -742,13 +687,11 @@ int PackTos::canUnpack()
return true;
}
/*************************************************************************
//
**************************************************************************/
void PackTos::unpack(OutputFile *fo)
{
void PackTos::unpack(OutputFile *fo) {
ibuf.alloc(ph.c_len);
obuf.allocForUncompression(ph.u_len);
@@ -756,18 +699,17 @@ void PackTos::unpack(OutputFile *fo)
fi->readx(ibuf, ph.c_len);
// decompress
decompress(ibuf,obuf);
decompress(ibuf, obuf);
// write original header & decompressed file
if (fo)
{
if (fo) {
unsigned overlay = file_size - (FH_SIZE + ih.fh_text + ih.fh_data);
if (ih.fh_reloc == 0 && overlay >= 4)
overlay -= 4; // this is our empty fixup
overlay -= 4; // this is our empty fixup
checkOverlay(overlay);
fo->write(obuf+ph.u_len-FH_SIZE, FH_SIZE); // orig. file_hdr
fo->write(obuf, ph.u_len-FH_SIZE); // orig. text+data+relocs
fo->write(obuf + ph.u_len - FH_SIZE, FH_SIZE); // orig. file_hdr
fo->write(obuf, ph.u_len - FH_SIZE); // orig. text+data+relocs
// copy any overlay
copyOverlay(fo, overlay, &obuf);