| 1 |
62 |
Agner |
/**************************** relink.cpp ***********************************
|
| 2 |
|
|
* Author: Agner Fog
|
| 3 |
|
|
* date created: 2017-12-07
|
| 4 |
|
|
* Last modified: 2018-03-30
|
| 5 |
|
|
* Version: 1.10
|
| 6 |
|
|
* Project: Binary tools for ForwardCom instruction set
|
| 7 |
|
|
* Description:
|
| 8 |
|
|
* This module contains the relinking feature of the linker.
|
| 9 |
|
|
*
|
| 10 |
|
|
* Copyright 2017-2020 GNU General Public License http://www.gnu.org/licenses
|
| 11 |
|
|
*****************************************************************************/
|
| 12 |
|
|
|
| 13 |
|
|
#include "stdafx.h"
|
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
// load executable file to be relinked
|
| 17 |
|
|
void CLinker::loadExeFile() {
|
| 18 |
|
|
|
| 19 |
|
|
// Read input file
|
| 20 |
|
|
const char * inputFileName = cmd.getFilename(cmd.inputFile);
|
| 21 |
|
|
inputFile.read(inputFileName);
|
| 22 |
|
|
if (err.number()) return;
|
| 23 |
|
|
inputFile.split();
|
| 24 |
|
|
if (!(inputFile.fileHeader.e_flags & EF_RELINKABLE)) {
|
| 25 |
|
|
err.submit(ERR_INPUT_NOT_RELINKABLE, inputFileName);
|
| 26 |
|
|
return;
|
| 27 |
|
|
}
|
| 28 |
|
|
|
| 29 |
|
|
// get names of modules and libraries to remove or replace
|
| 30 |
|
|
getReplaceNames();
|
| 31 |
|
|
|
| 32 |
|
|
#if 0
|
| 33 |
|
|
for (int i = 0; i < rnames.numEntries(); i++) {
|
| 34 |
|
|
printf("\n%4X %s", rnames[i].command, cmd.getFilename(rnames[i].filename));
|
| 35 |
|
|
}
|
| 36 |
|
|
#endif
|
| 37 |
|
|
|
| 38 |
|
|
markSectionsInInputFile();
|
| 39 |
|
|
}
|
| 40 |
|
|
|
| 41 |
|
|
// get names of modules and libraries to remove or replace
|
| 42 |
|
|
void CLinker::getReplaceNames() {
|
| 43 |
|
|
uint32_t i, j; // loop counter. command index
|
| 44 |
|
|
const char * fname; // file name or module name
|
| 45 |
|
|
SLCommand cmd2; // copy of command line item
|
| 46 |
|
|
numObjects = 0; // number of object files to add
|
| 47 |
|
|
numLibraries = 0; // number of library files to add
|
| 48 |
|
|
bool isLib; // file name indicates a library (.li or .a)
|
| 49 |
|
|
|
| 50 |
|
|
// make a list of removed and replaced modules and libraries
|
| 51 |
|
|
// and count number of object files and library files
|
| 52 |
|
|
for (i = 0; i < cmd.lcommands.numEntries(); i++) {
|
| 53 |
|
|
cmd2.command = 0;
|
| 54 |
|
|
// name of module
|
| 55 |
|
|
fname = cmd.getFilename(cmd.lcommands[i].filename);
|
| 56 |
|
|
|
| 57 |
|
|
// is it a library?
|
| 58 |
|
|
isLib = false;
|
| 59 |
|
|
// find last '.'
|
| 60 |
|
|
for (j = (int32_t)strlen(fname) - 1; j > 0; j--) {
|
| 61 |
|
|
if (fname[j] == '.') break;
|
| 62 |
|
|
}
|
| 63 |
|
|
if ((j > 0 && strncasecmp_(fname + j, ".li", 3) == 0) || fname[j+1] == 'a') {
|
| 64 |
|
|
isLib = true;
|
| 65 |
|
|
}
|
| 66 |
|
|
|
| 67 |
|
|
if ((cmd.lcommands[i].command & 0xFF) == CMDL_LINK_ADDMODULE) {
|
| 68 |
|
|
// remove path
|
| 69 |
|
|
cmd.lcommands[i].value = cmd.fileNameBuffer.pushString(removePath(fname));
|
| 70 |
|
|
|
| 71 |
|
|
if (isLib) { // this is a library
|
| 72 |
|
|
numLibraries++;
|
| 73 |
|
|
cmd.lcommands[i].command = CMDL_LINK_ADDLIBRARY | (cmd.lcommands[i].command & CMDL_LINK_RELINKABLE);
|
| 74 |
|
|
}
|
| 75 |
|
|
else { // assume that this is an object file
|
| 76 |
|
|
numObjects++;
|
| 77 |
|
|
}
|
| 78 |
|
|
cmd2 = cmd.lcommands[i];
|
| 79 |
|
|
cmd2.command |= CMDL_LINK_REPLACE;
|
| 80 |
|
|
}
|
| 81 |
|
|
if ((cmd.lcommands[i].command & 0xFF) == CMDL_LINK_ADDLIBMODULE) {
|
| 82 |
|
|
// object module from library file
|
| 83 |
|
|
// remove path
|
| 84 |
|
|
cmd.lcommands[i].value = cmd.fileNameBuffer.pushString(removePath(fname));
|
| 85 |
|
|
numObjects++;
|
| 86 |
|
|
cmd2 = cmd.lcommands[i];
|
| 87 |
|
|
cmd2.command |= CMDL_LINK_REPLACE;
|
| 88 |
|
|
}
|
| 89 |
|
|
if ((uint8_t)cmd.lcommands[i].command == CMDL_LINK_REMOVE) {
|
| 90 |
|
|
// remove module from relinkable file
|
| 91 |
|
|
// remove path
|
| 92 |
|
|
cmd.lcommands[i].value = cmd.fileNameBuffer.pushString(removePath(fname));
|
| 93 |
|
|
cmd2 = cmd.lcommands[i];
|
| 94 |
|
|
if (isLib) cmd2.command |= CMDL_LINK_ADDLIBRARY;
|
| 95 |
|
|
else cmd2.command |= CMDL_LINK_ADDMODULE;
|
| 96 |
|
|
}
|
| 97 |
|
|
// add command to rnames
|
| 98 |
|
|
if (cmd2.command) {
|
| 99 |
|
|
int32_t r = rnames.findFirst(cmd2);
|
| 100 |
|
|
if (r >= 0) {
|
| 101 |
|
|
// already in list. combine commands
|
| 102 |
|
|
rnames[r].command |= cmd2.command;
|
| 103 |
|
|
}
|
| 104 |
|
|
else {
|
| 105 |
|
|
// new name. add to list
|
| 106 |
|
|
rnames.addUnique(cmd2);
|
| 107 |
|
|
}
|
| 108 |
|
|
}
|
| 109 |
|
|
}
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
// check which sections to keep or remove in executable input file,
|
| 113 |
|
|
// and make list of modules and libraries to relink
|
| 114 |
|
|
void CLinker::markSectionsInInputFile() {
|
| 115 |
|
|
uint32_t sec; // section index
|
| 116 |
|
|
uint32_t rec; // rnames index
|
| 117 |
|
|
const char * modName; // module name
|
| 118 |
|
|
const char * libName; // library name
|
| 119 |
|
|
SLCommand cmdrec; // command record used only for name search
|
| 120 |
|
|
SRelinkModule relModul; // name of relinked module
|
| 121 |
|
|
zeroAllMembers(cmdrec);
|
| 122 |
|
|
|
| 123 |
|
|
for (sec = 0; sec < inputFile.sectionHeaders.numEntries(); sec++) {
|
| 124 |
|
|
ElfFwcShdr secHdr = inputFile.sectionHeaders[sec];
|
| 125 |
|
|
if (secHdr.sh_type == 0) continue;
|
| 126 |
|
|
inputFile.sectionHeaders[sec].sh_relink = 0;
|
| 127 |
|
|
|
| 128 |
|
|
if (secHdr.sh_module && secHdr.sh_module < inputFile.secStringTableLen) {
|
| 129 |
|
|
modName = inputFile.secStringTable + secHdr.sh_module;
|
| 130 |
|
|
}
|
| 131 |
|
|
else modName = "";
|
| 132 |
|
|
|
| 133 |
|
|
if (secHdr.sh_library && secHdr.sh_library < inputFile.secStringTableLen) {
|
| 134 |
|
|
libName = inputFile.secStringTable + secHdr.sh_library;
|
| 135 |
|
|
}
|
| 136 |
|
|
else libName = "";
|
| 137 |
|
|
|
| 138 |
|
|
// search for module name and library name in rnames
|
| 139 |
|
|
zeroAllMembers(cmdrec);
|
| 140 |
|
|
zeroAllMembers(relModul);
|
| 141 |
|
|
if (modName[0]) {
|
| 142 |
|
|
cmdrec.value = relModul.moduleName = cmd.fileNameBuffer.pushString(modName);
|
| 143 |
|
|
int32_t f1 = rnames.findFirst(cmdrec);
|
| 144 |
|
|
if (f1 >= 0) {
|
| 145 |
|
|
// module name is in list
|
| 146 |
|
|
rnames[f1].command |= CMD_NAME_FOUND; // mark name found
|
| 147 |
|
|
cmdrec.command = rnames[f1].command & ~CMDL_LINK_ADDLIBRARY;
|
| 148 |
|
|
if (inputFile.sectionHeaders[sec].sh_flags & SHF_RELINK) {
|
| 149 |
|
|
inputFile.sectionHeaders[sec].sh_relink = (uint8_t)rnames[f1].command; // mark section for replace or delete
|
| 150 |
|
|
}
|
| 151 |
|
|
else {
|
| 152 |
|
|
err.submit(ERR_CANT_RELINK_MODULE, modName);
|
| 153 |
|
|
}
|
| 154 |
|
|
}
|
| 155 |
|
|
}
|
| 156 |
|
|
if (libName[0]) {
|
| 157 |
|
|
cmdrec.value = relModul.libraryName = cmd.fileNameBuffer.pushString(libName);
|
| 158 |
|
|
int32_t f2 = rnames.findFirst(cmdrec);
|
| 159 |
|
|
if (f2 >= 0) {
|
| 160 |
|
|
// library name is in list
|
| 161 |
|
|
rnames[f2].command |= CMD_NAME_FOUND; // mark name found
|
| 162 |
|
|
cmdrec.command = rnames[f2].command | CMDL_LINK_ADDLIBRARY;
|
| 163 |
|
|
if (inputFile.sectionHeaders[sec].sh_flags & SHF_RELINK) {
|
| 164 |
|
|
inputFile.sectionHeaders[sec].sh_relink |= (uint8_t)rnames[f2].command; // mark section for replace or delete
|
| 165 |
|
|
}
|
| 166 |
|
|
else {
|
| 167 |
|
|
err.submit(ERR_CANT_RELINK_LIBRARY, modName);
|
| 168 |
|
|
}
|
| 169 |
|
|
}
|
| 170 |
|
|
}
|
| 171 |
|
|
|
| 172 |
|
|
// add module or library to relinkModules list unless it is removed or replaced
|
| 173 |
|
|
if (cmdrec.value && !(cmdrec.command & (CMDL_LINK_REMOVE | CMDL_LINK_REPLACE))) {
|
| 174 |
|
|
relinkModules.addUnique(relModul);
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
#if 0 // testing only: list sections
|
| 178 |
|
|
const char * secName; // section name
|
| 179 |
|
|
if (secHdr.sh_name < inputFile.stringBuffer.dataSize()) {
|
| 180 |
|
|
secName = inputFile.stringBuffer.getString(secHdr.sh_name);
|
| 181 |
|
|
}
|
| 182 |
|
|
else secName = "";
|
| 183 |
|
|
printf("\n: %2i >> %s %s %s %2X", sec, secName, libName, modName, inputFile.sectionHeaders[sec].sh_relink);
|
| 184 |
|
|
#endif
|
| 185 |
|
|
}
|
| 186 |
|
|
#if 0 // testing only: list kept modules and libraries
|
| 187 |
|
|
for (rec = 0; rec < relinkModules.numEntries(); rec++) {
|
| 188 |
|
|
printf("\n# %s:%s", cmd.getFilename(relinkModules[rec].libraryName), cmd.getFilename(relinkModules[rec].moduleName));
|
| 189 |
|
|
}
|
| 190 |
|
|
#endif
|
| 191 |
|
|
|
| 192 |
|
|
// check if all remove/replace records in rnames have been matched by sections in input file
|
| 193 |
|
|
for (rec = 0; rec < rnames.numEntries(); rec++) {
|
| 194 |
|
|
if ((rnames[rec].command & CMDL_LINK_REMOVE) && !(rnames[rec].command & CMD_NAME_FOUND)) {
|
| 195 |
|
|
// unmatched name
|
| 196 |
|
|
modName = cmd.getFilename(uint32_t(rnames[rec].value));
|
| 197 |
|
|
if (rnames[rec].command & CMDL_LINK_ADDMODULE) {
|
| 198 |
|
|
err.submit(ERR_RELINK_MODULE_NOT_FOUND, modName);
|
| 199 |
|
|
}
|
| 200 |
|
|
else {
|
| 201 |
|
|
err.submit(ERR_RELINK_LIBRARY_NOT_FOUND, modName);
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|
| 204 |
|
|
}
|
| 205 |
|
|
}
|
| 206 |
|
|
|
| 207 |
|
|
// extract a module from executable input file
|
| 208 |
|
|
void CLinker::extractModule(CELF & modul, uint32_t libname, uint32_t name) {
|
| 209 |
|
|
// libname: library name as index into cmd.fileNameBuffer. Zero if not a library member
|
| 210 |
|
|
// name: module name as index into cmd.fileNameBuffer. Zero to build a module of all non-relinkable sections
|
| 211 |
|
|
|
| 212 |
|
|
uint32_t sec; // section index in inputFile
|
| 213 |
|
|
uint32_t seci; // section index in modul
|
| 214 |
|
|
uint32_t sym; // symbol index in inputFile
|
| 215 |
|
|
uint32_t symi; // symbol index in modul
|
| 216 |
|
|
uint32_t rel; // relocation index in inputFile
|
| 217 |
|
|
const char * modName; // module name of section
|
| 218 |
|
|
const char * libName; // library name of library
|
| 219 |
|
|
const char * modName1; // module name to search for
|
| 220 |
|
|
const char * libName1; // library name to search for
|
| 221 |
|
|
uint32_t * symp; // pointer to symbol index
|
| 222 |
|
|
modName1 = cmd.getFilename(name); // will be "" if name = 0
|
| 223 |
|
|
libName1 = cmd.getFilename(libname); // will be "" if libname = 0
|
| 224 |
|
|
CDynamicArray<uint32_t> symbolTranslate; // list for translating symbol indexes from inputFile to modul
|
| 225 |
|
|
CDynamicArray<uint32_t> sectionTranslate; // list for translating section indexes from inputFile to modul
|
| 226 |
|
|
CDynamicArray<SSymbol2> externalSymbols; // list of external symbols referenced by current module
|
| 227 |
|
|
CDynamicArray<uint32_t> symbolTranslate2; // list for translating symbol indexes from externalSymbols to modul
|
| 228 |
|
|
ElfFwcSym symrec; // symbol record
|
| 229 |
|
|
|
| 230 |
|
|
// prepare translation of symbol indexes
|
| 231 |
|
|
symbolTranslate.setNum(inputFile.symbols.numEntries());
|
| 232 |
|
|
sectionTranslate.setNum(inputFile.sectionHeaders.numEntries());
|
| 233 |
|
|
zeroAllMembers(symrec);
|
| 234 |
|
|
modul.addSymbol(symrec, inputFile.stringBuffer); // make symbol zero empty
|
| 235 |
|
|
|
| 236 |
|
|
// loop through sections of input file
|
| 237 |
|
|
for (sec = 0; sec < inputFile.sectionHeaders.numEntries(); sec++) {
|
| 238 |
|
|
ElfFwcShdr secHdr = inputFile.sectionHeaders[sec];
|
| 239 |
|
|
if (secHdr.sh_type == 0) continue; // skip first empty section
|
| 240 |
|
|
if (secHdr.sh_flags & SHF_RELINK) {
|
| 241 |
|
|
// relinkable section. check name match
|
| 242 |
|
|
if (secHdr.sh_module && secHdr.sh_module < inputFile.secStringTableLen) {
|
| 243 |
|
|
modName = inputFile.secStringTable + secHdr.sh_module;
|
| 244 |
|
|
}
|
| 245 |
|
|
else continue;
|
| 246 |
|
|
if (strcmp(modName, modName1) != 0) continue; // name doesn't match
|
| 247 |
|
|
// check library name
|
| 248 |
|
|
if (secHdr.sh_library && secHdr.sh_library < inputFile.secStringTableLen) {
|
| 249 |
|
|
libName = inputFile.secStringTable + secHdr.sh_library;
|
| 250 |
|
|
if (libname == 0) continue; // library member not requested
|
| 251 |
|
|
if (strcmp(libName, libName1) != 0) continue; // library name doesn't match
|
| 252 |
|
|
}
|
| 253 |
|
|
else if (libname) continue; // not a library member
|
| 254 |
|
|
}
|
| 255 |
|
|
else {
|
| 256 |
|
|
// non-relinkable section
|
| 257 |
|
|
if (name || libname) continue; // name = 0 for collecting non-relinkable sections
|
| 258 |
|
|
// non-relinkable sections must have fixed addresses relative to each other because the
|
| 259 |
|
|
// corresponding relocation records are not preserved.
|
| 260 |
|
|
// Insert address relative to ip_base, datap_base, or threadp_base:
|
| 261 |
|
|
switch (secHdr.sh_flags & SHF_BASEPOINTER) {
|
| 262 |
|
|
case SHF_IP:
|
| 263 |
|
|
secHdr.sh_addr = secHdr.sh_addr - inputFile.fileHeader.e_ip_base;
|
| 264 |
|
|
break;
|
| 265 |
|
|
case SHF_DATAP:
|
| 266 |
|
|
secHdr.sh_addr = secHdr.sh_addr - inputFile.fileHeader.e_datap_base;
|
| 267 |
|
|
break;
|
| 268 |
|
|
case SHF_THREADP:
|
| 269 |
|
|
secHdr.sh_addr = secHdr.sh_addr - inputFile.fileHeader.e_threadp_base;
|
| 270 |
|
|
break;
|
| 271 |
|
|
}
|
| 272 |
|
|
}
|
| 273 |
|
|
// all sections that do not match name and libname have been skipped now
|
| 274 |
|
|
if (secHdr.sh_flags & SHF_AUTOGEN) continue; // auto-generated section. will be re-made
|
| 275 |
|
|
|
| 276 |
|
|
// add this section to the module
|
| 277 |
|
|
seci = modul.addSection(secHdr, inputFile.stringBuffer, inputFile.dataBuffer);
|
| 278 |
|
|
sectionTranslate[sec] = seci;
|
| 279 |
|
|
|
| 280 |
|
|
// find symbols in this section
|
| 281 |
|
|
for (sym = 0; sym < inputFile.symbols.numEntries(); sym++) {
|
| 282 |
|
|
if (inputFile.symbols[sym].st_section == sec) {
|
| 283 |
|
|
// save symbol
|
| 284 |
|
|
symrec = inputFile.symbols[sym];
|
| 285 |
|
|
symrec.st_section = seci;
|
| 286 |
|
|
symi = modul.addSymbol(symrec, inputFile.stringBuffer);
|
| 287 |
|
|
symbolTranslate[sym] = symi;
|
| 288 |
|
|
}
|
| 289 |
|
|
}
|
| 290 |
|
|
}
|
| 291 |
|
|
|
| 292 |
|
|
// find relocations in any section belonging to this module
|
| 293 |
|
|
for (rel = 0; rel < inputFile.relocations.numEntries(); rel++) {
|
| 294 |
|
|
sec = inputFile.relocations[rel].r_section;
|
| 295 |
|
|
if (sec < sectionTranslate.numEntries()) {
|
| 296 |
|
|
seci = sectionTranslate[sec];
|
| 297 |
|
|
if (seci) {
|
| 298 |
|
|
ElfFwcReloc reloc = inputFile.relocations[rel];
|
| 299 |
|
|
reloc.r_section = seci;
|
| 300 |
|
|
// loop to cover both symbol and reference symbol in relocation record
|
| 301 |
|
|
for (int i = 0; i < 2; i++) {
|
| 302 |
|
|
symp = i ? &reloc.r_refsym : &reloc.r_sym;
|
| 303 |
|
|
if (*symp) {
|
| 304 |
|
|
// there is a symbol index
|
| 305 |
|
|
if (*symp < symbolTranslate.numEntries() && symbolTranslate[*symp]) {
|
| 306 |
|
|
// symbol is in same module. Translate to index in modul
|
| 307 |
|
|
*symp = symbolTranslate[*symp];
|
| 308 |
|
|
}
|
| 309 |
|
|
else if (*symp < inputFile.symbols.numEntries()) {
|
| 310 |
|
|
// symbol is external. make external symbol record
|
| 311 |
|
|
SSymbol2 symbol2 = inputFile.symbols[*symp];
|
| 312 |
|
|
symbol2.st_section = 0; // make symbol external
|
| 313 |
|
|
symbol2.st_value = 0;
|
| 314 |
|
|
// put symbol name in global symbolNameBuffer for the purpose of sorting
|
| 315 |
|
|
if (symbol2.st_name >= inputFile.stringBuffer.dataSize()) {
|
| 316 |
|
|
err.submit(ERR_ELF_INDEX_RANGE); return;
|
| 317 |
|
|
}
|
| 318 |
|
|
const char * symname = (char*)inputFile.stringBuffer.buf() + symbol2.st_name;
|
| 319 |
|
|
symbol2.st_name = symbolNameBuffer.pushString(symname);
|
| 320 |
|
|
// add to list of external symbols, avoid duplicates
|
| 321 |
|
|
externalSymbols.addUnique(symbol2);
|
| 322 |
|
|
// remember that symbol index is not resolved yet
|
| 323 |
|
|
*symp |= 0x80000000;
|
| 324 |
|
|
}
|
| 325 |
|
|
else err.submit(ERR_ELF_INDEX_RANGE);
|
| 326 |
|
|
}
|
| 327 |
|
|
}
|
| 328 |
|
|
// save relocation record
|
| 329 |
|
|
modul.addRelocation(reloc);
|
| 330 |
|
|
}
|
| 331 |
|
|
}
|
| 332 |
|
|
}
|
| 333 |
|
|
// add external symbols to modul and remember new indexes
|
| 334 |
|
|
symbolTranslate2.setNum(externalSymbols.numEntries());
|
| 335 |
|
|
for (sym = 0; sym < externalSymbols.numEntries(); sym++) {
|
| 336 |
|
|
symrec = externalSymbols[sym];
|
| 337 |
|
|
if (symrec.st_bind == STB_UNRESOLVED) {
|
| 338 |
|
|
symrec.st_bind = STB_GLOBAL; // unresolved symbol from incomplete executable. attempt to resolve it again
|
| 339 |
|
|
}
|
| 340 |
|
|
symbolTranslate2[sym] = modul.addSymbol(symrec, symbolNameBuffer);
|
| 341 |
|
|
}
|
| 342 |
|
|
|
| 343 |
|
|
// resolve external symbol indexes in relocation records in new module
|
| 344 |
|
|
for (rel = 0; rel < modul.relocations.numEntries(); rel++) {
|
| 345 |
|
|
ElfFwcReloc & modulReloc = modul.relocations[rel];
|
| 346 |
|
|
// loop to cover both symbol and reference symbol in relocation record
|
| 347 |
|
|
for (int i = 0; i < 2; i++) {
|
| 348 |
|
|
uint32_t * symp = i ? &modulReloc.r_refsym : &modulReloc.r_sym;
|
| 349 |
|
|
if (*symp & 0x80000000) {
|
| 350 |
|
|
// find symbol index in externalSymbols
|
| 351 |
|
|
SSymbol2 sym2 = inputFile.symbols[*symp & 0x7FFFFFFF];
|
| 352 |
|
|
const char * symname = (char*)inputFile.stringBuffer.buf() + sym2.st_name;
|
| 353 |
|
|
sym2.st_name = symbolNameBuffer.pushString(symname);
|
| 354 |
|
|
int32_t eindex = externalSymbols.findFirst(sym2);
|
| 355 |
|
|
if (eindex < 0) {
|
| 356 |
|
|
err.submit(ERR_INDEX_OUT_OF_RANGE); // should not occur
|
| 357 |
|
|
return;
|
| 358 |
|
|
}
|
| 359 |
|
|
*symp = symbolTranslate2[(uint32_t)eindex];
|
| 360 |
|
|
}
|
| 361 |
|
|
}
|
| 362 |
|
|
}
|
| 363 |
|
|
ElfFwcEhdr head;
|
| 364 |
|
|
zeroAllMembers(head);
|
| 365 |
|
|
modul.join(&head);
|
| 366 |
|
|
}
|
| 367 |
|
|
|
| 368 |
|
|
// count number of modules and libraries to reuse when relinking
|
| 369 |
|
|
void CLinker::countReusedModules() {
|
| 370 |
|
|
uint32_t rec; // record index
|
| 371 |
|
|
const char * libname; // library name
|
| 372 |
|
|
const char * lastlibname = ""; // library name of preceding record
|
| 373 |
|
|
|
| 374 |
|
|
numRelinkObjects = 1; // including modules1[0] which contains all reused non-relinkable modules
|
| 375 |
|
|
numRelinkLibraries = 0; // number of relinkable modules
|
| 376 |
|
|
// this is counted as one, even if unused
|
| 377 |
|
|
if (cmd.job != CMDL_JOB_RELINK) return; // not relinking
|
| 378 |
|
|
|
| 379 |
|
|
// loop through relinkModules list
|
| 380 |
|
|
for (rec = 0; rec < relinkModules.numEntries(); rec++) {
|
| 381 |
|
|
if (relinkModules[rec].libraryName) {
|
| 382 |
|
|
libname = cmd.getFilename(relinkModules[rec].libraryName);
|
| 383 |
|
|
if (rec > 0 && strcmp(libname, lastlibname) == 0) {
|
| 384 |
|
|
continue; // multiple modules from same library: count only once
|
| 385 |
|
|
}
|
| 386 |
|
|
lastlibname = libname;
|
| 387 |
|
|
numRelinkLibraries++;
|
| 388 |
|
|
}
|
| 389 |
|
|
else if (relinkModules[rec].moduleName) numRelinkObjects++;
|
| 390 |
|
|
}
|
| 391 |
|
|
}
|
| 392 |
|
|
|
| 393 |
|
|
// get all relinked objects into modules1 metabuffer
|
| 394 |
|
|
void CLinker::getRelinkObjects() {
|
| 395 |
|
|
uint32_t rec; // record index
|
| 396 |
|
|
uint32_t mod = 0; // module index
|
| 397 |
|
|
uint32_t sec = 0; // section index
|
| 398 |
|
|
const char * modname; // module name
|
| 399 |
|
|
// join all non-relinkable sections into first entry
|
| 400 |
|
|
extractModule(modules1[0], 0, 0);
|
| 401 |
|
|
mod++;
|
| 402 |
|
|
#if 1 // testing: write module file
|
| 403 |
|
|
modules1[0].write("ff.ob");
|
| 404 |
|
|
#endif
|
| 405 |
|
|
|
| 406 |
|
|
// mark all sections for fixed position because they have already been relocated
|
| 407 |
|
|
for (sec = 0; sec < modules1[0].sectionHeaders.numEntries(); sec++) {
|
| 408 |
|
|
modules1[0].sectionHeaders[sec].sh_flags |= SHF_FIXED;
|
| 409 |
|
|
}
|
| 410 |
|
|
if (cmd.verbose && numRelinkObjects > 1) {
|
| 411 |
|
|
printf("\nReusing object modules:");
|
| 412 |
|
|
}
|
| 413 |
|
|
|
| 414 |
|
|
// loop through relinkModules list to search for non-library modules
|
| 415 |
|
|
for (rec = 0; rec < relinkModules.numEntries(); rec++) {
|
| 416 |
|
|
if (relinkModules[rec].libraryName == 0 && relinkModules[rec].moduleName != 0) {
|
| 417 |
|
|
extractModule(modules1[mod], 0, relinkModules[rec].moduleName);
|
| 418 |
|
|
modules1[mod].moduleName = relinkModules[rec].moduleName;
|
| 419 |
|
|
modules1[mod].relinkable = true;
|
| 420 |
|
|
extractModuleToFile(modules1[mod]); // possibly extract to file
|
| 421 |
|
|
mod++;
|
| 422 |
|
|
// write name
|
| 423 |
|
|
if (cmd.verbose) {
|
| 424 |
|
|
modname = cmd.getFilename(relinkModules[rec].moduleName);
|
| 425 |
|
|
printf(" %s", modname);
|
| 426 |
|
|
}
|
| 427 |
|
|
}
|
| 428 |
|
|
}
|
| 429 |
|
|
}
|
| 430 |
|
|
|
| 431 |
|
|
// extract a module from relinkable file if requested
|
| 432 |
|
|
void CLinker::extractModuleToFile(CELF & modu) {
|
| 433 |
|
|
// search for extract command
|
| 434 |
|
|
if (!(cmd.libraryOptions & CMDL_LIBRARY_EXTRACTMEM)) return;
|
| 435 |
|
|
|
| 436 |
|
|
uint32_t i; // loop counter
|
| 437 |
|
|
const char * modname1; // module name
|
| 438 |
|
|
const char * modname2; // module name on command line
|
| 439 |
|
|
bool extract = false;
|
| 440 |
|
|
modname1 = cmd.getFilename(modu.moduleName);
|
| 441 |
|
|
if (modname1[0] == 0) return;
|
| 442 |
|
|
|
| 443 |
|
|
if (cmd.libraryOptions == CMDL_LIBRARY_EXTRACTALL) extract = true;
|
| 444 |
|
|
for (i = 0; i < cmd.lcommands.numEntries(); i++) {
|
| 445 |
|
|
if (cmd.lcommands[i].command == CMDL_LINK_EXTRACT) {
|
| 446 |
|
|
// name of module
|
| 447 |
|
|
modname2 = cmd.getFilename(cmd.lcommands[i].filename);
|
| 448 |
|
|
if (strcmp(modname1, modname2) == 0) {
|
| 449 |
|
|
extract = true;
|
| 450 |
|
|
break;
|
| 451 |
|
|
}
|
| 452 |
|
|
}
|
| 453 |
|
|
}
|
| 454 |
|
|
if (!extract) return; // no request for extracting this module
|
| 455 |
|
|
// make new name = old name prefixed by "x_"
|
| 456 |
|
|
uint32_t newname = cmd.fileNameBuffer.dataSize();
|
| 457 |
|
|
cmd.fileNameBuffer.push("x_", 2);
|
| 458 |
|
|
cmd.fileNameBuffer.pushString(modname1);
|
| 459 |
|
|
modname2 = cmd.getFilename(newname);
|
| 460 |
|
|
modu.write(modname2);
|
| 461 |
|
|
}
|
| 462 |
|
|
|
| 463 |
|
|
// recover relinkable library modules
|
| 464 |
|
|
void CLinker::getRelinkLibraries() {
|
| 465 |
|
|
if (cmd.job != CMDL_JOB_RELINK) return; // not relinking
|
| 466 |
|
|
uint32_t rec; // record index
|
| 467 |
|
|
const char * libname; // library name
|
| 468 |
|
|
const char * nextlibname = ""; // library name of next record
|
| 469 |
|
|
const char * modname; // module name
|
| 470 |
|
|
uint32_t iLibrary = numLibraries + 1; // library index
|
| 471 |
|
|
CELF modul; // recovered library module
|
| 472 |
|
|
|
| 473 |
|
|
if (cmd.verbose && numRelinkLibraries) {
|
| 474 |
|
|
printf("\nRecovering library modules:");
|
| 475 |
|
|
}
|
| 476 |
|
|
|
| 477 |
|
|
// loop through relinkModules list, looking for library modules
|
| 478 |
|
|
for (rec = 0; rec < relinkModules.numEntries(); rec++) {
|
| 479 |
|
|
if (relinkModules[rec].libraryName && relinkModules[rec].moduleName) {
|
| 480 |
|
|
libname = cmd.getFilename(relinkModules[rec].libraryName);
|
| 481 |
|
|
modname = cmd.getFilename(relinkModules[rec].moduleName);
|
| 482 |
|
|
if (cmd.verbose) {
|
| 483 |
|
|
printf(" %s:%s", libname, modname);
|
| 484 |
|
|
}
|
| 485 |
|
|
// extract library module from relinkable input file
|
| 486 |
|
|
modul.reset();
|
| 487 |
|
|
extractModule(modul, relinkModules[rec].libraryName, relinkModules[rec].moduleName);
|
| 488 |
|
|
modul.moduleName = relinkModules[rec].moduleName;
|
| 489 |
|
|
|
| 490 |
|
|
modul.library = iLibrary; //??
|
| 491 |
|
|
|
| 492 |
|
|
extractModuleToFile(modul); // extract to file if requested
|
| 493 |
|
|
// build internal library
|
| 494 |
|
|
libraries[iLibrary].addELF(modul);
|
| 495 |
|
|
if (rec + 1 < relinkModules.numEntries()) {
|
| 496 |
|
|
nextlibname = cmd.getFilename(relinkModules[rec+1].libraryName);
|
| 497 |
|
|
}
|
| 498 |
|
|
else nextlibname = "?/";
|
| 499 |
|
|
if (strcmp(libname, nextlibname) != 0) {
|
| 500 |
|
|
// last module for this library. Finish internal library
|
| 501 |
|
|
libraries[iLibrary].makeInternalLibrary();
|
| 502 |
|
|
libraries[iLibrary].libraryName = relinkModules[rec].libraryName;
|
| 503 |
|
|
libraries[iLibrary].relinkable = true;
|
| 504 |
|
|
iLibrary++;
|
| 505 |
|
|
}
|
| 506 |
|
|
}
|
| 507 |
|
|
}
|
| 508 |
|
|
}
|
| 509 |
|
|
|
| 510 |
|
|
// write feedback to console
|
| 511 |
|
|
void CLinker::feedBackText2() {
|
| 512 |
|
|
if (!(cmd.verbose)) return; // write feedback only if verbose
|
| 513 |
|
|
uint32_t i; // loop counter
|
| 514 |
|
|
const char * name; // name of file or module
|
| 515 |
|
|
const char * libname; // name of library
|
| 516 |
|
|
bool written = false; // message has been written
|
| 517 |
|
|
|
| 518 |
|
|
// search for removed objects
|
| 519 |
|
|
for (i = 0; i < rnames.numEntries(); i++) {
|
| 520 |
|
|
if (((uint8_t)rnames[i].command & CMDL_LINK_REMOVE) && !((uint8_t)rnames[i].command & CMDL_LINK_ADDLIBRARY)) {
|
| 521 |
|
|
if (!written) printf("\nRemoving object files:");
|
| 522 |
|
|
written = true;
|
| 523 |
|
|
name = cmd.getFilename(uint32_t(rnames[i].value));
|
| 524 |
|
|
printf(" %s", name);
|
| 525 |
|
|
if (!(rnames[i].command & CMD_NAME_FOUND)) printf(" failed!");
|
| 526 |
|
|
}
|
| 527 |
|
|
}
|
| 528 |
|
|
// list of added objects have already been written to console.
|
| 529 |
|
|
// write replaced and removed objects
|
| 530 |
|
|
written = false;
|
| 531 |
|
|
// search for replaced objects
|
| 532 |
|
|
for (i = 0; i < rnames.numEntries(); i++) {
|
| 533 |
|
|
if ((rnames[i].command & CMDL_LINK_ADDMODULE) && (rnames[i].command & CMD_NAME_FOUND)
|
| 534 |
|
|
&&!(rnames[i].command & CMDL_LINK_REMOVE)) {
|
| 535 |
|
|
if (!written) printf("\nReplacing object files:");
|
| 536 |
|
|
written = true;
|
| 537 |
|
|
name = cmd.getFilename(uint32_t(rnames[i].value));
|
| 538 |
|
|
printf(" %s", name);
|
| 539 |
|
|
}
|
| 540 |
|
|
}
|
| 541 |
|
|
written = false;
|
| 542 |
|
|
// search for removed libraries
|
| 543 |
|
|
for (i = 0; i < rnames.numEntries(); i++) {
|
| 544 |
|
|
if ((uint8_t)rnames[i].command == (CMDL_LINK_REMOVE | CMDL_LINK_ADDLIBRARY)) {
|
| 545 |
|
|
if (!written) printf("\nRemoving library files:");
|
| 546 |
|
|
written = true;
|
| 547 |
|
|
name = cmd.getFilename(rnames[i].filename);
|
| 548 |
|
|
printf(" %s", name);
|
| 549 |
|
|
if (!(rnames[i].command & CMD_NAME_FOUND)) printf(" failed!");
|
| 550 |
|
|
}
|
| 551 |
|
|
}
|
| 552 |
|
|
written = false;
|
| 553 |
|
|
// search for added libraries
|
| 554 |
|
|
for (i = 0; i < rnames.numEntries(); i++) {
|
| 555 |
|
|
if ((uint8_t)rnames[i].command == (CMDL_LINK_REPLACE | CMDL_LINK_ADDLIBRARY)
|
| 556 |
|
|
&& !(rnames[i].command & CMD_NAME_FOUND)) {
|
| 557 |
|
|
if (!written) printf("\nAdding library files:");
|
| 558 |
|
|
written = true;
|
| 559 |
|
|
name = cmd.getFilename(rnames[i].filename);
|
| 560 |
|
|
printf(" %s", name);
|
| 561 |
|
|
}
|
| 562 |
|
|
}
|
| 563 |
|
|
written = false;
|
| 564 |
|
|
// search for replaced libraries
|
| 565 |
|
|
for (i = 0; i < rnames.numEntries(); i++) {
|
| 566 |
|
|
if ((rnames[i].command & CMDL_LINK_REPLACE)
|
| 567 |
|
|
&& (rnames[i].command & CMDL_LINK_ADDLIBRARY)
|
| 568 |
|
|
&& (rnames[i].command & CMD_NAME_FOUND)) {
|
| 569 |
|
|
if (!written) printf("\nReplacing library files:");
|
| 570 |
|
|
written = true;
|
| 571 |
|
|
name = cmd.getFilename(rnames[i].filename);
|
| 572 |
|
|
printf(" %s", name);
|
| 573 |
|
|
}
|
| 574 |
|
|
}
|
| 575 |
|
|
written = false;
|
| 576 |
|
|
// search for added library members
|
| 577 |
|
|
for (i = 0; i < libmodules.numEntries(); i++) {
|
| 578 |
|
|
uint32_t lib = libmodules[i].library & 0x7FFFFFFF;
|
| 579 |
|
|
if (!written) printf("\nUsing library members:");
|
| 580 |
|
|
written = true;
|
| 581 |
|
|
libname = cmd.getFilename(libraries[lib].libraryName);
|
| 582 |
|
|
name = libraries[lib].getMemberName(libmodules[i].offset);
|
| 583 |
|
|
printf(" %s:%s", libname, name);
|
| 584 |
|
|
}
|
| 585 |
|
|
}
|