Skip to content

Commit

Permalink
fixed uarm executables load failing with oddly linked elfs
Browse files Browse the repository at this point in the history
reverted elf2uarm and fixed a minor bug
  • Loading branch information
mellotanica committed May 7, 2015
1 parent 3bb5350 commit c5795a9
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 188 deletions.
12 changes: 6 additions & 6 deletions armProc/aout.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
//#define N_AOUT_HDR_ENT 43

/* Magic Number Tags */
#define DISKFILEID 0x0053504D
#define TAPEFILEID 0x0153504D
#define BIOSFILEID 0x0253504D
#define COREFILEID 0x0353504D
#define AOUTFILEID 0x0453504D
#define STABFILEID 0x4153504D
#define DISKFILEID 0x004D5241
#define TAPEFILEID 0x014D5241
#define BIOSFILEID 0x024D5241
#define COREFILEID 0x034D5241
#define AOUTFILEID 0x044D5241
#define STABFILEID 0x414D5241

#endif //UARM_AOUT_H
4 changes: 2 additions & 2 deletions armProc/blockdev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "armProc/blockdev_params.h"
#include "services/utility.h"
#include "armProc/blockdev.h"

#include "armProc/aout.h"

// This method returns an empty (unitialized) 512 byte Block
Block::Block()
Expand Down Expand Up @@ -116,7 +116,7 @@ DriveParams::DriveParams(FILE * diskFile, SWord * fileOfs)
Block * blk = new Block();

rewind(diskFile);
if (blk->ReadBlock(diskFile, 0) || blk->getWord(0) != DISKFILEID)
if (blk->ReadBlock(diskFile, 0) || blk->getWord(0) != DISKFILEID)
// errors in file reading or disk file magic number missing
ret = 0;
else
Expand Down
8 changes: 0 additions & 8 deletions armProc/blockdev_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
#ifndef UARM_BLOCKDEV_PARAMS_H
#define UARM_BLOCKDEV_PARAMS_H

// file recognition tags (magic numbers)
#define DISKFILEID 0x0053504D
#define TAPEFILEID 0x0153504D
#define BIOSFILEID 0x0253504D
#define COREFILEID 0x0353504D
#define AOUTFILEID 0x0453504D
#define STABFILEID 0x4153504D

// tape markers
#define TAPESTART 3
#define TAPEEOB 2
Expand Down
35 changes: 21 additions & 14 deletions armProc/bus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,31 @@ bool systemBus::loadBIOS(char *buffer, Word size){

bool systemBus::loadRAM(char *buffer, Word size, bool kernel){
if(kernel){
Word address = RAMBASEADDR;
Word address = ((Byte) buffer[(AOUT_HE_TEXT_VADDR*WS)])| ((Byte) buffer[(AOUT_HE_TEXT_VADDR*WS)+1]) << 8 | ((Byte) buffer[(AOUT_HE_TEXT_VADDR*WS)+2]) << 16 | ((Byte) buffer[(AOUT_HE_TEXT_VADDR*WS)+3]) << 24;
Word dataVAddr = 0, textSize = 0;
bool textSet = false;
//copy provided data only for legit ram addresses
for(Word i = 0; i < size; i++, address++){
if(textSet && i >= textSize){
address = dataVAddr;
textSet = false;
if(address >= RAMBASEADDR){
if(textSet && i >= textSize){
address = dataVAddr;
textSet = false;
}
if(i/4 == (AOUT_HE_DATA_VADDR+1) && i%4 == 0){
Word taddr = address-4;
readW(&taddr, &dataVAddr);
}
if(!textSet && (i/4 == (AOUT_HE_TEXT_FILESZ+1) && i%4 == 0)){
Word taddr = address-4;
readW(&taddr, &textSize);
textSet = true;
}
writeB(&address, (Byte) buffer[i]);
}
if(i/4 == (AOUT_HE_DATA_VADDR+1) && i%4 == 0){
Word taddr = address-4;
readW(&taddr, &dataVAddr);
}
if(!textSet && (i/4 == (AOUT_HE_TEXT_FILESZ+1) && i%4 == 0)){
Word taddr = address-4;
readW(&taddr, &textSize);
textSet = true;
}
writeB(&address, (Byte) buffer[i]);
}
//now make sure core header is at its place at the beginning of ram
for(Word i = 0, address = RAMBASEADDR; i < WS*N_AOUT_HDR_ENT; i++, address++){
writeB(&address, (Byte)buffer[i]);
}
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions armProc/device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "armProc/vde_network.h"
#include "armProc/machine.h"

#include "armProc/aout.h"


// last operation result description
HIDDEN const char* const opResult[2] = {
Expand Down
Binary file modified facilities/BIOS.rom.uarm
Binary file not shown.
Binary file modified facilities/crti.o
Binary file not shown.
Binary file modified facilities/crtso.o
Binary file not shown.
Binary file modified facilities/libuarm.o
Binary file not shown.
3 changes: 1 addition & 2 deletions qarm/qarm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ bool qarm::openRAM(){
error->show();
return false;
}
sz -= 4;
if(sz <= 0 || !mac->getBus()->loadRAM(buffer+4, (Word) sz, true)){
if(sz <= 0 || !mac->getBus()->loadRAM(buffer, (Word) sz, true)){
QarmMessageBox *error = new QarmMessageBox(QarmMessageBox::CRITICAL, "Error", "Problems while loading Core file", this);
error->show();
return false;
Expand Down
162 changes: 7 additions & 155 deletions services/elf2arm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,11 @@
#include "armProc/aout.h"
#include "facilities/arch.h"

struct span_s {
Elf32_Addr begin;
Elf32_Addr end;
Elf32_Off offset;
};

struct phskip_s {
bool skip;
Elf32_Off amount;
};

/*
* Functions throughtout this module close over this global!
*/
static Elf* elf;
static Elf32_Ehdr* elfHeader;
static Elf32_Shdr *section_headers;
static Elf32_Phdr *program_headers;
static struct phskip_s *phskip;
static long archive_file_offset;

static const size_t kBlockSize = 4096;

Expand Down Expand Up @@ -139,141 +124,6 @@ const char* fileName = NULL;
// Be verbose
static bool verbose = false;

void printShdr(){
int i;
Elf32_Shdr *iter;
printf("sections header:\n");
for(i = 0, iter = section_headers; i < elfHeader->e_shnum; i++, iter++){
printf("[%d] name: 0x%08x, type: 0x%08x, addraign: 0x%08x\n addr: 0x%08x, offset: 0x%08x, size: 0x%08x\n",
i, iter->sh_name, iter->sh_type, iter->sh_addralign,
iter->sh_addr, iter->sh_offset, iter->sh_size);
}
}

void printPhdr(){
int i;
Elf32_Phdr *iter;
printf("program header:\n");
for(i = 0, iter = program_headers; i < elfHeader->e_phnum; i++, iter++){
printf("[%d] paddr: 0x%08x, memsz: 0x%08x, offset:: 0x%08x\n",
i, iter->p_paddr, iter->p_memsz, iter->p_offset);
}
}

void printSpans(struct span_s *span, int num){
int i;
struct span_s *iter;
printf("spans:\n");
for(i = 0, iter = span; i < num; i++, iter++){
printf("[%d] begin: 0x%08x, end: 0x%08x, offset: 0x%08x\n",
i, iter->begin, iter->end, iter->offset);
}
}

void printSkips(){
int i;
struct phskip_s *iter;
printf("skips:\n");
for(i = 0, iter = phskip; i < elfHeader->e_phnum; i++, iter++){
printf("[%d] skip: %d, amount: 0x%08x\n",
i, iter->skip, iter->amount);
}
}

// from binutils' readelf.c
static void *
get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb)
{
void * mvar;

if (size == 0 || nmemb == 0)
return NULL;

if (fseek (file, archive_file_offset + offset, SEEK_SET))
return NULL;

mvar = var;
if (mvar == NULL)
{
/* Check for overflow. */
if (nmemb < (~(size_t) 0 - 1) / size)
/* + 1 so that we can '\0' terminate invalid string table sections. */
mvar = malloc (size * nmemb + 1);

if (mvar == NULL)
return NULL;

((char *) mvar)[size * nmemb] = '\0';
}

if (fread (mvar, size, nmemb, file) != nmemb)
{
if (mvar != var)
free (mvar);
return NULL;
}

return mvar;
}

void setupHeaders(const char * filename){
FILE *file = fopen(filename, "rb");
int i, j, spans_num;

if(file == NULL){
fatalError("Cannot access %s: %s", fileName, strerror(errno));
}

//do i have to move this??
archive_file_offset = 0;

section_headers = (Elf32_Shdr *) get_data (NULL, file, elfHeader->e_shoff,
elfHeader->e_shentsize, elfHeader->e_shnum);
program_headers = (Elf32_Phdr *) get_data (NULL, file, elfHeader->e_phoff,
elfHeader->e_phentsize, elfHeader->e_phnum);

struct span_s *tspan = (struct span_s *) calloc(elfHeader->e_shnum, sizeof(struct span_s));

for(i = 0; i < elfHeader->e_shnum; i++){
if(section_headers[i].sh_type == SHT_PROGBITS && section_headers[i].sh_addr >= RAM_BASE){
tspan[spans_num].begin = section_headers[i].sh_addr;
tspan[spans_num].end = tspan[spans_num].begin + section_headers[i].sh_size;
tspan[spans_num].offset = section_headers[i].sh_offset;
spans_num ++;
}
}

printShdr();
printSpans(tspan, spans_num);

phskip = (struct phskip_s *)calloc(elfHeader->e_phnum, sizeof(struct phskip_s));
for(i = 0; i < elfHeader->e_phnum; i++){
phskipip[i].skip = false;
phskip[i].amount = 0;
for(j = 0; j < spans_num; j++){
if(program_headers[i].p_paddr <= tspan[j].begin && (program_headers[i].p_paddr + program_headers[i].p_memsz) >= tspan[j].end){ //contained section
if(program_headers[i].p_paddr == tspan[j].begin){ //one section begins at the beginning of the program data
phskip[i].skip = false;
break;
}
if(phskip[i].amount == 0){ //no offset set for this section
phskip[i].skip = true;
phskip[i].amount = tspan[j].offset;
continue;
}
if(phskip[i].amount > tspan[j].offset){ //section begins earlier than previously checked ones
phskip[i].amount = tspan[j].offset;
continue;
}
}
}
}

free(tspan);

printPhdr();
printSkips();
}

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -334,8 +184,6 @@ int main(int argc, char** argv)
if (elfHeader->e_machine != EM_ARM)
fatalError("%s: invalid machine type (ARM expected)", fileName);

setupHeaders(fileName);

switch (fileId) {
case BIOSFILEID:
elf2bios();
Expand Down Expand Up @@ -473,7 +321,10 @@ static void elf2aout(bool isCore)

uint32_t header[N_AOUT_HDR_ENT];
std::fill_n(header, N_AOUT_HDR_ENT, 0);
header[AOUT_HE_TAG] = AOUTFILEID;
if(isCore)
header[AOUT_HE_TAG] = COREFILEID;
else
header[AOUT_HE_TAG] = AOUTFILEID;

// Set program entry
header[AOUT_HE_ENTRY] = elfHeader->e_entry;
Expand Down Expand Up @@ -580,18 +431,19 @@ static void elf2aout(bool isCore)
if (file == NULL)
fatalError("Cannot create a.out file `%s'", outName.c_str());

/* i don't need this...
// If it's a core file, write the RRF padding first (padding clears the bios reserved frame)
if (isCore) {
uint32_t tag = toTargetEndian(COREFILEID);
if (fwrite(&tag, sizeof(tag), 1, file) != 1)
fatalError("Error writing a.out file `%s'", outName.c_str());
/* i don't need this...
uint32_t pad = 0;
for (size_t i = 0; i < 1024; i++)
if (fwrite(&pad, sizeof(pad), 1, file) != 1)
fatalError("Error writing a.out file `%s'", outName.c_str());
*/
}
*/

// Write the segments, finally.
if (fwrite(textBuf, 1, header[AOUT_HE_TEXT_FILESZ], file) != header[AOUT_HE_TEXT_FILESZ])
Expand Down
2 changes: 1 addition & 1 deletion services/symbol_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <boost/bind.hpp>

#include "armProc/const.h"
#include "armProc/blockdev_params.h"
#include "armProc/aout.h"
#include "services/disass.h"
#include "services/utility.h"
#include "services/error.h"
Expand Down

0 comments on commit c5795a9

Please sign in to comment.