2016-01-14 15:23:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1987 Fujitsu
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2014-05-04 05:20:34 +00:00
|
|
|
/*
|
|
|
|
relocate.c -- Routines to relocate relocatable code for the Slinky
|
|
|
|
linker.
|
|
|
|
|
|
|
|
Chip Morningstar -- Lucasfilm Ltd.
|
|
|
|
|
|
|
|
15-March-1985
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "slinkyTypes.h"
|
|
|
|
#include "slinkyGlobals.h"
|
2016-01-24 06:57:12 +00:00
|
|
|
#include "relocate.h"
|
|
|
|
#include "debugPrint.h"
|
|
|
|
#include "errorStuff.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "map.h"
|
2014-05-04 05:20:34 +00:00
|
|
|
|
|
|
|
#define isUndefined(symbol) (((symbol)->symbolClass & ~SYMBOL_EXTERNAL) == 0)
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
removeZeroPageFromFreeList(void)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
while (freeSegmentList->segmentEndAddress <= 0x100)
|
|
|
|
freeSegmentList = freeSegmentList->nextFreeSegment;
|
|
|
|
if (freeSegmentList->segmentStartAddress < 0x100)
|
|
|
|
freeSegmentList->segmentStartAddress = 0x100;
|
|
|
|
}
|
|
|
|
|
|
|
|
addressType
|
2016-01-24 07:13:44 +00:00
|
|
|
align(addressType address, int alignment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
if (alignment == 0)
|
|
|
|
return(address);
|
|
|
|
else
|
|
|
|
return(((address + alignment - 1) / alignment) * alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
addressType
|
2016-01-24 07:13:44 +00:00
|
|
|
constrain(addressType address, int size, addressType constraint)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
if (constraint == 0)
|
|
|
|
return(address);
|
|
|
|
else if (address/constraint != (address + size - 1)/constraint)
|
|
|
|
return(align(address, constraint));
|
|
|
|
else
|
|
|
|
return(address);
|
|
|
|
}
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
moveRelocationBase(addressType newBase)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
freeSegmentEntryType *freePtr;
|
|
|
|
freeSegmentEntryType *newFreePtr;
|
|
|
|
|
|
|
|
freePtr = freeSegmentList;
|
|
|
|
while (freePtr != NULL && newBase > freePtr->segmentEndAddress)
|
|
|
|
freePtr = freePtr->nextFreeSegment;
|
|
|
|
if (freePtr == NULL || freePtr->segmentStartAddress == newBase) {
|
|
|
|
effectiveFreeSegmentList = freePtr;
|
|
|
|
} else {
|
|
|
|
newFreePtr = typeAlloc(freeSegmentEntryType);
|
|
|
|
newFreePtr->segmentEndAddress = freePtr->segmentEndAddress;
|
|
|
|
freePtr->segmentEndAddress = newBase - 1;
|
|
|
|
newFreePtr->segmentStartAddress = newBase;
|
|
|
|
newFreePtr->nextFreeSegment = freePtr->nextFreeSegment;
|
|
|
|
freePtr->nextFreeSegment = newFreePtr;
|
|
|
|
effectiveFreeSegmentList = newFreePtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addressType
|
2016-01-24 07:13:44 +00:00
|
|
|
allocateRelocatable(codeSegmentHeaderType *codeSegment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
freeSegmentEntryType *freePtr;
|
|
|
|
freeSegmentEntryType *previousPtr;
|
|
|
|
freeSegmentEntryType *newFreePtr;
|
|
|
|
int size;
|
|
|
|
addressType effectiveStartAddress;
|
|
|
|
|
|
|
|
freePtr = effectiveFreeSegmentList;
|
|
|
|
previousPtr = NULL;
|
|
|
|
size = codeSegment->segmentEndAddress - codeSegment->
|
|
|
|
segmentStartAddress + 1;
|
|
|
|
while (freePtr != NULL) {
|
|
|
|
effectiveStartAddress = align(freePtr->segmentStartAddress,
|
|
|
|
codeSegment->alignment);
|
|
|
|
effectiveStartAddress = constrain(effectiveStartAddress, size,
|
|
|
|
codeSegment->constraint);
|
|
|
|
if (freePtr->segmentEndAddress - effectiveStartAddress + 1 <
|
|
|
|
size) {
|
|
|
|
previousPtr = freePtr;
|
|
|
|
freePtr = freePtr->nextFreeSegment;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* if we made it to here, we got a winner! */
|
|
|
|
if (effectiveStartAddress == freePtr->segmentStartAddress) {
|
|
|
|
if (effectiveStartAddress + size - 1 == freePtr->
|
|
|
|
segmentEndAddress) {
|
|
|
|
if (previousPtr == NULL)
|
|
|
|
freeSegmentList = freePtr->nextFreeSegment;
|
|
|
|
else
|
|
|
|
previousPtr->nextFreeSegment = freePtr->
|
|
|
|
nextFreeSegment;
|
|
|
|
} else {
|
|
|
|
freePtr->segmentStartAddress += size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (effectiveStartAddress + size - 1 == freePtr->
|
|
|
|
segmentEndAddress) {
|
|
|
|
freePtr->segmentEndAddress -= size;
|
|
|
|
} else {
|
|
|
|
newFreePtr = typeAlloc(freeSegmentEntryType);
|
|
|
|
newFreePtr->nextFreeSegment = freePtr->nextFreeSegment;
|
|
|
|
freePtr->nextFreeSegment = newFreePtr;
|
|
|
|
newFreePtr->segmentEndAddress =freePtr->segmentEndAddress;
|
|
|
|
freePtr->segmentEndAddress = effectiveStartAddress - 1;
|
|
|
|
newFreePtr->segmentStartAddress = effectiveStartAddress +
|
|
|
|
size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(effectiveStartAddress);
|
|
|
|
}
|
|
|
|
return((addressType) -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
relocateOneCodeSegment(codeSegmentHeaderType *codeSegment, addressType targetLocation)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
int relocationOffset;
|
|
|
|
|
|
|
|
relocationOffset = targetLocation - codeSegment->segmentStartAddress;
|
|
|
|
if (verbose)
|
|
|
|
printf(" 0x%04x:0x%04x --> 0x%04x", codeSegment->
|
|
|
|
segmentStartAddress, codeSegment->segmentEndAddress,
|
|
|
|
targetLocation);
|
|
|
|
codeSegment->relocationOffset = relocationOffset;
|
|
|
|
codeSegment->segmentStartAddress += relocationOffset;
|
|
|
|
codeSegment->segmentEndAddress += relocationOffset;
|
|
|
|
if (!packFlag) {
|
|
|
|
moveRelocationBase(codeSegment->segmentEndAddress+1);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
printf(" (0x%04x:0x%04x)\n", codeSegment->segmentStartAddress,
|
|
|
|
codeSegment->segmentEndAddress);
|
|
|
|
installSegment(codeSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
relocatem(void)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
objectFileListType *inputFileList;
|
|
|
|
addressType targetLocation;
|
|
|
|
codeSegmentHeaderType *codeSegment;
|
|
|
|
|
|
|
|
reserveReservations();
|
|
|
|
removeZeroPageFromFreeList();
|
|
|
|
for (inputFileList = objectFileList; inputFileList != NULL;
|
|
|
|
inputFileList = inputFileList->nextObjectFile) {
|
|
|
|
if (inputFileList->name != NULL) {
|
|
|
|
currentFileName = inputFileList->name;
|
|
|
|
if (verbose)
|
|
|
|
printf("%s:\n", inputFileList->name);
|
|
|
|
for (codeSegment = inputFileList->codeSegments; codeSegment !=
|
|
|
|
NULL; codeSegment = codeSegment->nextSegment) {
|
|
|
|
if (codeSegment->segmentMode == MODE_RELOCATABLE) {
|
|
|
|
if ((targetLocation=allocateRelocatable(codeSegment))
|
|
|
|
== (addressType) -1) {
|
|
|
|
error(NO_PLACE_TO_PUT_CODE_SEGMENT_ERROR,
|
|
|
|
codeSegment->segmentStartAddress,
|
|
|
|
inputFileList->name);
|
|
|
|
} else {
|
|
|
|
relocateOneCodeSegment(codeSegment,
|
|
|
|
targetLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /* null name encodes new relocation base */
|
|
|
|
moveRelocationBase(inputFileList->symbolCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codeSegmentHeaderType *
|
2016-01-24 07:13:44 +00:00
|
|
|
matchModes(symbolType *symbol, codeSegmentHeaderType *codeSegment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
while (codeSegment!=NULL && ((codeSegment->segmentMode==MODE_ABSOLUTE
|
|
|
|
&& !(symbol->symbolClass & SYMBOL_ABSOLUTE)) ||
|
|
|
|
(codeSegment->segmentMode==MODE_RELOCATABLE &&
|
|
|
|
!(symbol->symbolClass & SYMBOL_RELOCATABLE) &&
|
|
|
|
!(symbol->symbolClass & SYMBOL_ABSOLUTE))))
|
|
|
|
codeSegment = codeSegment->nextSegment;
|
|
|
|
return(codeSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-01-24 07:13:44 +00:00
|
|
|
matchedModes(symbolType *symbol, codeSegmentHeaderType *codeSegment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
return(((symbol->symbolClass & SYMBOL_ABSOLUTE) && codeSegment->
|
|
|
|
segmentMode == MODE_ABSOLUTE) || ((symbol->symbolClass &
|
|
|
|
SYMBOL_RELOCATABLE) && codeSegment->segmentMode ==
|
|
|
|
MODE_RELOCATABLE));
|
|
|
|
}
|
|
|
|
|
|
|
|
codeSegmentHeaderType *
|
2016-01-24 07:13:44 +00:00
|
|
|
synchronizeCodeSegment(symbolType *symbol, codeSegmentHeaderType *codeSegment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
codeSegment = matchModes(symbol, codeSegment);
|
|
|
|
while (codeSegment != NULL && codeSegment->nextSegment != NULL &&
|
|
|
|
codeSegment->segmentEndAddress - codeSegment->
|
|
|
|
relocationOffset < symbol->symbolValue &&
|
|
|
|
matchedModes(symbol, codeSegment)) {
|
|
|
|
codeSegment = codeSegment->nextSegment;
|
|
|
|
}
|
|
|
|
return(codeSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
handleGlobalSymbol(symbolType *symbol)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
valueSymbol(symbolType *symbol, codeSegmentHeaderType *codeSegment)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
if (symbol->symbolClass & SYMBOL_ABSOLUTE) {
|
|
|
|
return;
|
|
|
|
} else if (symbol->symbolClass & SYMBOL_RELOCATABLE) {
|
|
|
|
symbol->symbolValue += codeSegment->relocationOffset;
|
|
|
|
} else {
|
|
|
|
printf("Super botcho! Undefined symbol in ValueSymbol.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
symbolType *
|
2016-01-24 07:13:44 +00:00
|
|
|
lookupGlobalSymbol(char *symbolName)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
int guess;
|
|
|
|
int top;
|
|
|
|
int bottom;
|
|
|
|
int compare;
|
|
|
|
int oldTop;
|
|
|
|
int oldBottom;
|
|
|
|
|
|
|
|
top = oldBottom = globalSymbolCount;
|
|
|
|
bottom = oldTop = 0;
|
|
|
|
while (top != bottom && top != oldTop && bottom != oldBottom) {
|
|
|
|
guess = (top + bottom) >> 1;
|
|
|
|
compare = strcmplc(globalSymbolTable[guess]->symbolName,
|
|
|
|
symbolName);
|
|
|
|
if (compare == 0)
|
|
|
|
return(globalSymbolTable[guess]);
|
|
|
|
else if (compare < 0) {
|
|
|
|
oldBottom = bottom;
|
|
|
|
bottom = guess;
|
|
|
|
} else {
|
|
|
|
oldTop = top;
|
|
|
|
top = guess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
valueUndefinedSymbol(symbolType *symbol)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
symbolType *globalSymbol;
|
|
|
|
|
|
|
|
if ((globalSymbol = lookupGlobalSymbol(symbol->symbolName)) == NULL) {
|
|
|
|
error(UNDEFINED_SYMBOL_ERROR, symbol->symbolName);
|
|
|
|
} else {
|
|
|
|
symbol->symbolClass = globalSymbol->symbolClass;
|
|
|
|
symbol->symbolValue = globalSymbol->symbolValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-24 07:13:44 +00:00
|
|
|
valuem(void)
|
2014-05-04 05:20:34 +00:00
|
|
|
{
|
|
|
|
objectFileListType *inputFileList;
|
|
|
|
codeSegmentHeaderType *codeSegmentPtr;
|
|
|
|
symbolType *symbolPtr;
|
|
|
|
int symbolCount;
|
|
|
|
|
|
|
|
totalSymbolCount = 0;
|
|
|
|
for (inputFileList = objectFileList; inputFileList != NULL;
|
|
|
|
inputFileList = inputFileList->nextObjectFile) {
|
|
|
|
if (inputFileList->name != NULL) {
|
|
|
|
currentFileName = inputFileList->name;
|
|
|
|
if (debug)
|
|
|
|
printf("\nSymbols with values (%s):\n", inputFileList->
|
|
|
|
name);
|
|
|
|
codeSegmentPtr = inputFileList->codeSegments;
|
|
|
|
for (symbolCount = 0; symbolCount<inputFileList->symbolCount;
|
|
|
|
symbolCount++) {
|
|
|
|
symbolPtr = inputFileList->symbolTable[symbolCount];
|
|
|
|
if (isUndefined(symbolPtr)) {
|
|
|
|
/* inputFileList->undefinedSymbols = symbolPtr;*/
|
|
|
|
inputFileList->undefinedSymbols = &(inputFileList->symbolTable[symbolCount]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
codeSegmentPtr = synchronizeCodeSegment(symbolPtr,
|
|
|
|
codeSegmentPtr);
|
|
|
|
valueSymbol(symbolPtr, codeSegmentPtr);
|
|
|
|
if (debug)
|
|
|
|
printSymbol(symbolCount, symbolPtr);
|
|
|
|
}
|
|
|
|
inputFileList->baseSymbolCount = symbolCount;
|
|
|
|
inputFileList->symbolCount -= symbolCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (inputFileList = objectFileList; inputFileList != NULL;
|
|
|
|
inputFileList = inputFileList->nextObjectFile) {
|
|
|
|
if (inputFileList->name != NULL) {
|
|
|
|
currentFileName = inputFileList->name;
|
|
|
|
if (debug)
|
|
|
|
printf("\nGlobal symbols with values (%s):\n",
|
|
|
|
inputFileList->name);
|
|
|
|
for (symbolCount = 0; symbolCount <inputFileList->symbolCount;
|
|
|
|
symbolCount++) {
|
|
|
|
symbolPtr = inputFileList->undefinedSymbols[symbolCount];
|
|
|
|
valueUndefinedSymbol(symbolPtr);
|
|
|
|
if (debug)
|
|
|
|
printSymbol(symbolCount, symbolPtr);
|
|
|
|
}
|
|
|
|
inputFileList->symbolCount = inputFileList->baseSymbolCount;
|
|
|
|
totalSymbolCount += inputFileList->baseSymbolCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (debug)
|
|
|
|
printf("\nTotal symbol count = %d\n", totalSymbolCount);
|
|
|
|
if (produceLoadMap)
|
|
|
|
outputLoadMap();
|
|
|
|
}
|