Line 6... |
Line 6... |
* Main translation unit for the LXP32 assembler/linker.
|
* Main translation unit for the LXP32 assembler/linker.
|
*/
|
*/
|
|
|
#include "assembler.h"
|
#include "assembler.h"
|
#include "linker.h"
|
#include "linker.h"
|
|
#include "utils.h"
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <fstream>
|
#include <fstream>
|
#include <vector>
|
#include <vector>
|
#include <string>
|
#include <string>
|
Line 23... |
Line 24... |
struct Options {
|
struct Options {
|
enum OutputFormat {Bin,Textio,Dec,Hex};
|
enum OutputFormat {Bin,Textio,Dec,Hex};
|
|
|
bool compileOnly=false;
|
bool compileOnly=false;
|
std::string outputFileName;
|
std::string outputFileName;
|
|
std::string mapFileName;
|
std::vector<std::string> includeSearchDirs;
|
std::vector<std::string> includeSearchDirs;
|
LinkableObject::Word base=0;
|
LinkableObject::Word base=0;
|
std::size_t align=4;
|
std::size_t align=4;
|
std::size_t imageSize=0;
|
std::size_t imageSize=0;
|
OutputFormat fmt=Bin;
|
OutputFormat fmt=Bin;
|
Line 36... |
Line 38... |
os<<std::endl;
|
os<<std::endl;
|
os<<"Usage:"<<std::endl;
|
os<<"Usage:"<<std::endl;
|
os<<" "<<program<<" [ option(s) | input file(s) ]"<<std::endl<<std::endl;
|
os<<" "<<program<<" [ option(s) | input file(s) ]"<<std::endl<<std::endl;
|
|
|
os<<"Options:"<<std::endl;
|
os<<"Options:"<<std::endl;
|
os<<" -a <align> Section alignment (default: 4)"<<std::endl;
|
os<<" -a <align> Object alignment (default: 4)"<<std::endl;
|
os<<" -b <addr> Base address (default: 0)"<<std::endl;
|
os<<" -b <addr> Base address (default: 0)"<<std::endl;
|
os<<" -c Compile only (don't link)"<<std::endl;
|
os<<" -c Compile only (don't link)"<<std::endl;
|
os<<" -f <fmt> Output file format (see below)"<<std::endl;
|
os<<" -f <fmt> Output file format (see below)"<<std::endl;
|
os<<" -h, --help Display a short help message"<<std::endl;
|
os<<" -h, --help Display a short help message"<<std::endl;
|
os<<" -i <dir> Add directory to the list of directories used to search"<<std::endl;
|
os<<" -i <dir> Add directory to the list of directories used to search"<<std::endl;
|
os<<" for included files (multiple directories can be specified)"<<std::endl;
|
os<<" for included files (multiple directories can be specified)"<<std::endl;
|
|
os<<" -m <file> Generate map file"<<std::endl;
|
os<<" -o <file> Output file name"<<std::endl;
|
os<<" -o <file> Output file name"<<std::endl;
|
os<<" -s <size> Output image size"<<std::endl;
|
os<<" -s <size> Output image size"<<std::endl;
|
os<<" -- Do not interpret subsequent arguments as options"<<std::endl;
|
os<<" -- Do not interpret subsequent arguments as options"<<std::endl;
|
os<<std::endl;
|
os<<std::endl;
|
os<<"Section alignment and image size must be multiples of 4."<<std::endl;
|
os<<"Object alignment must be a power of two and can't be less than 4."<<std::endl;
|
os<<"Base address must be a multiple of section alignment."<<std::endl;
|
os<<"Base address must be a multiple of object alignment."<<std::endl;
|
|
os<<"Image size must be a multiple of 4."<<std::endl;
|
os<<std::endl;
|
os<<std::endl;
|
|
|
os<<"Output file formats:"<<std::endl;
|
os<<"Output file formats:"<<std::endl;
|
os<<" bin Raw binary image (default)"<<std::endl;
|
os<<" bin Raw binary image (default)"<<std::endl;
|
os<<" textio Text representation of binary data. Supported by"<<std::endl;
|
os<<" textio Text representation of binary data. Supported by"<<std::endl;
|
Line 65... |
Line 69... |
static const char *id="LinkableObject";
|
static const char *id="LinkableObject";
|
static std::size_t idSize=std::strlen(id);
|
static std::size_t idSize=std::strlen(id);
|
|
|
std::ifstream in(filename,std::ios_base::in);
|
std::ifstream in(filename,std::ios_base::in);
|
if(!in) return false;
|
if(!in) return false;
|
|
if(in.tellg()==static_cast<std::ifstream::pos_type>(-1))
|
|
return false; // the stream is not seekable
|
|
|
std::vector<char> buf(idSize);
|
std::vector<char> buf(idSize);
|
in.read(buf.data(),idSize);
|
in.read(buf.data(),idSize);
|
if(static_cast<std::size_t>(in.gcount())!=idSize) return false;
|
if(static_cast<std::size_t>(in.gcount())!=idSize) return false;
|
if(std::memcmp(buf.data(),id,idSize)) return false;
|
if(std::memcmp(buf.data(),id,idSize)) return false;
|
Line 82... |
Line 88... |
bool baseSpecified=false;
|
bool baseSpecified=false;
|
bool formatSpecified=false;
|
bool formatSpecified=false;
|
bool noMoreOptions=false;
|
bool noMoreOptions=false;
|
|
|
std::cout<<"LXP32 Platform Assembler and Linker"<<std::endl;
|
std::cout<<"LXP32 Platform Assembler and Linker"<<std::endl;
|
std::cout<<"Copyright (c) 2016 by Alex I. Kuznetsov"<<std::endl;
|
std::cout<<"Copyright (c) 2016-2019 by Alex I. Kuznetsov"<<std::endl;
|
|
|
if(argc<=1) {
|
if(argc<=1) {
|
displayUsage(std::cout,argv[0]);
|
displayUsage(std::cout,argv[0]);
|
return 0;
|
return 0;
|
}
|
}
|
Line 99... |
Line 105... |
displayUsage(std::cerr,argv[0]);
|
displayUsage(std::cerr,argv[0]);
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|
try {
|
try {
|
options.align=std::stoul(argv[i],nullptr,0);
|
options.align=std::stoul(argv[i],nullptr,0);
|
if(options.align%4!=0||options.align==0) throw std::exception();
|
if(!Utils::isPowerOf2(options.align)) throw std::exception();
|
|
if(options.align<4) throw std::exception();
|
alignmentSpecified=true;
|
alignmentSpecified=true;
|
}
|
}
|
catch(std::exception &) {
|
catch(std::exception &) {
|
throw std::runtime_error("Invalid section alignment");
|
throw std::runtime_error("Invalid object alignment");
|
}
|
}
|
}
|
}
|
else if(!strcmp(argv[i],"-b")) {
|
else if(!strcmp(argv[i],"-b")) {
|
if(++i==argc) {
|
if(++i==argc) {
|
displayUsage(std::cerr,argv[0]);
|
displayUsage(std::cerr,argv[0]);
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|
try {
|
try {
|
options.base=std::stoul(argv[i],nullptr,0);
|
options.base=std::stoul(argv[i],nullptr,0);
|
//if(options.base%4!=0) throw std::exception();
|
|
baseSpecified=true;
|
baseSpecified=true;
|
}
|
}
|
catch(std::exception &) {
|
catch(std::exception &) {
|
throw std::runtime_error("Invalid base address");
|
throw std::runtime_error("Invalid base address");
|
}
|
}
|
Line 146... |
Line 152... |
displayUsage(std::cerr,argv[0]);
|
displayUsage(std::cerr,argv[0]);
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|
options.includeSearchDirs.push_back(argv[i]);
|
options.includeSearchDirs.push_back(argv[i]);
|
}
|
}
|
|
else if(!strcmp(argv[i],"-m")) {
|
|
if(++i==argc) {
|
|
displayUsage(std::cerr,argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
options.mapFileName=argv[i];
|
|
}
|
else if(!strcmp(argv[i],"-o")) {
|
else if(!strcmp(argv[i],"-o")) {
|
if(++i==argc) {
|
if(++i==argc) {
|
displayUsage(std::cerr,argv[0]);
|
displayUsage(std::cerr,argv[0]);
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|
Line 170... |
Line 183... |
}
|
}
|
else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
|
else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
|
}
|
}
|
|
|
if(options.base%options.align!=0)
|
if(options.base%options.align!=0)
|
throw std::runtime_error("Base address must be a multiple of section alignment");
|
throw std::runtime_error("Base address must be a multiple of object alignment");
|
|
|
if(options.compileOnly) {
|
if(options.compileOnly) {
|
if(alignmentSpecified)
|
if(alignmentSpecified)
|
std::cerr<<"Warning: Section alignment is ignored in compile-only mode"<<std::endl;
|
std::cerr<<"Warning: Object alignment is ignored in compile-only mode"<<std::endl;
|
if(baseSpecified)
|
if(baseSpecified)
|
std::cerr<<"Warning: Base address is ignored in compile-only mode"<<std::endl;
|
std::cerr<<"Warning: Base address is ignored in compile-only mode"<<std::endl;
|
if(formatSpecified)
|
if(formatSpecified)
|
std::cerr<<"Warning: Output format is ignored in compile-only mode"<<std::endl;
|
std::cerr<<"Warning: Output format is ignored in compile-only mode"<<std::endl;
|
if(options.imageSize>0)
|
if(options.imageSize>0)
|
std::cerr<<"Warning: Image size is ignored in compile-only mode"<<std::endl;
|
std::cerr<<"Warning: Image size is ignored in compile-only mode"<<std::endl;
|
|
if(!options.mapFileName.empty())
|
|
std::cerr<<"Warning: Map file is not generated in compile-only mode"<<std::endl;
|
}
|
}
|
|
|
if(inputFiles.empty())
|
if(inputFiles.empty())
|
throw std::runtime_error("No input files were specified");
|
throw std::runtime_error("No input files were specified");
|
|
|
Line 276... |
Line 291... |
catch(std::exception &ex) {
|
catch(std::exception &ex) {
|
writer->abort();
|
writer->abort();
|
std::cerr<<"Linker error: "<<ex.what()<<std::endl;
|
std::cerr<<"Linker error: "<<ex.what()<<std::endl;
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|
|
|
|
std::cout<<writer->size()/4<<" words written"<<std::endl;
|
|
|
|
if(!options.mapFileName.empty()) {
|
|
std::ofstream out(options.mapFileName);
|
|
if(!out) throw std::runtime_error("Cannot open file \""+options.mapFileName+"\" for writing");
|
|
linker.generateMap(out);
|
|
}
|
}
|
}
|
catch(std::exception &ex) {
|
catch(std::exception &ex) {
|
std::cerr<<"Error: "<<ex.what()<<std::endl;
|
std::cerr<<"Error: "<<ex.what()<<std::endl;
|
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
}
|
}
|