Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for relocations #39

Merged
merged 15 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ArchC-Core/AcInstruction.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ AcInstruction >> annotationAdd: anObject [
].
]

{ #category : #annotations }
AcInstruction >> annotationAddAll: aCollection [
aCollection notNil ifTrue: [
aCollection do: [:each | self annotationAdd: each ]
].
]

{ #category : #annotations }
AcInstruction >> annotationAddOrReplace: anObject [
annotations isNil ifTrue: [
Expand Down Expand Up @@ -197,3 +204,17 @@ AcInstruction >> isPseudoInstruction [
AcInstruction >> name [
^ self subclassResponsibility
]

{ #category : #accessing }
AcInstruction >> relocation [
"Return relocation entry associated with this instruction."

"Implementation note:

Only processor instructions can have relocation entries associated,
this method is defined in here to simplify coding (no need to
test for processor instruction first, merely for non-nil relocation).
"

^ nil
]
36 changes: 36 additions & 0 deletions src/ArchC-Core/AcIntLimits.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Class {
#name : #AcIntLimits,
#superclass : #SharedPool,
#classVars : [
'INT16_MAX',
'INT16_MIN',
'INT32_MAX',
'INT32_MIN',
'INT64_MAX',
'INT64_MIN',
'UINT16_MAX',
'UINT16_MIN',
'UINT32_MAX',
'UINT32_MIN',
'UINT64_MAX',
'UINT64_MIN'
],
#category : #'ArchC-Core-Utilities'
}

{ #category : #initialization }
AcIntLimits class >> initialize [
INT16_MIN := -16r8000.
INT16_MAX := 16r7FFF.
INT32_MIN := -16r80000000.
INT32_MAX := 16r7FFFFFFF.
INT64_MIN := -16r8000000000000000.
INT64_MAX := 16r7FFFFFFFFFFFFFFF.

UINT16_MIN := 0.
UINT16_MAX := 16rFFFF.
UINT32_MIN := 0.
UINT32_MAX := 16rFFFFFFFF.
UINT64_MIN := 0.
UINT64_MAX := 16rFFFFFFFFFFFFFFFF.
]
208 changes: 208 additions & 0 deletions src/ArchC-Core/AcRelocation.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
Class {
#name : #AcRelocation,
#superclass : #Object,
#instVars : [
'symbol',
'addend'
],
#classVars : [
'AddressOutOfRange'
],
#category : #'ArchC-Core-Relocations'
}

{ #category : #initialization }
AcRelocation class >> initialize [
AddressOutOfRange := 16r1FFFFFFFFFFFFFFFF
]

{ #category : #queries }
AcRelocation class >> isAbstract [
"Return if this class is an abstract class.
True is returned here for myself only; false for subclasses.
Abstract subclasses must redefine this again."

^ self == AcRelocation.
]

{ #category : #queries }
AcRelocation class >> isValidForInstruction: insn [
"Return `true` this relocation is valid for fiven instruction (or instruction declaration).
Otherwise, return `false`."

^ self subclassResponsibility
]

{ #category : #'instance creation' }
AcRelocation class >> new [
^ self shouldNotImplement. "Use #symbol: instead"
]

{ #category : #'instance creation' }
AcRelocation class >> symbol: symbol [
^ self symbol: symbol addend: 0
]

{ #category : #'instance creation' }
AcRelocation class >> symbol: symbol addend: addend [
^ self basicNew initializeWithSymbol: symbol addend: addend
]

{ #category : #accessing }
AcRelocation >> addend [
^ addend
]

{ #category : #accessing }
AcRelocation >> addend: anInteger [
self assert: anInteger isInteger.

addend := anInteger.
]

{ #category : #'processing - simplified' }
AcRelocation >> calculateS: s A: a P: p [
"Calculate relocation value (either relative of absolute, depending
on relocation type) and return it. If the value is out of range,
return special value `AddressOutOfRange`.

Parameters:

s ... final address of the symbol in memory
a ... addend field of the relocation entry
p ... position of the relocation (address of instruction being relocated)

NOTE: this method is part of a 'simplified' relocation processing.
If the relocation requires GOT / TOC entry or PLT entry (or any other
value other than those given), you highly likely want to override
#fixup:symbols:relocation: method!
"
^ self subclassResponsibility
]

{ #category : #'printing & storing' }
AcRelocation >> disassembleOn: aStream [
aStream nextPutAll: self class name.
aStream space.
aStream nextPutAll: symbol asString.
addend ~~ 0 ifTrue: [
aStream space.
aStream nextPut:(addend < 0 ifTrue: [ $- ] ifFalse: [ $+ ]).
aStream space.
addend abs printOn: aStream
].
]

{ #category : #displaying }
AcRelocation >> displayOn: aStream [
aStream nextPutAll: self class name.
aStream space.
aStream nextPutAll: symbol asString.
addend ~~ 0 ifTrue: [
aStream nextPutAll: ' + '.
addend printOn: aStream
].
]

{ #category : #displaying }
AcRelocation >> displayStringOn: aStream [
"For Pharo compatibility"
self displayOn: aStream
]

{ #category : #'processing - simplified' }
AcRelocation >> fixup: memory at: address with: value [
"Patch instruction at `address` with `value`."

self subclassResponsibility
]

{ #category : #processing }
AcRelocation >> fixup: memory symbols: symbols relocations: relocations [
"Process this relocation and patch (binary) instructions.

If relocation symbol address is not (yet) known (that is, not present
in `symbols` dictionary), throw `AcRelocationSymbolUnresolved`.

If the code cannot be fixed up because of value over/underflow, throw
`AcRelocationOverflow`.

If the code cannot be fixed up for any other reasons, throw generic
`AcRelocationFailed`.

Parameters:

* `memory` is a byte-array like object containing binary code.
* `symbols` is a dictionary mapping (known) symbols to their respective
addresses.
* `relocations` is a dictionary mapping relocations to their respective
addresses.

NOTE: the below (default) code implements so-called 'simplified' processing
for relocations that do not need any GOT / TOC entries or PLT entries and
do not perform relaxation. If the relocation type requires any of that,
you likely need to override this method.
"
| s a p v |

(symbols includesKey: symbol) ifFalse: [
AcRelocationSymbolUnresolved new
relocation: self;
signal: 'Relocation symbol is unresolved (symbol address is not known)'.
^ self
].
(relocations includesKey: self) ifFalse: [
AcRelocationFailed new
relocation: self;
signal: 'Relocation position is not known!'. "This should not happen!"
^ self
].


s := symbols at: symbol.
a := addend.
p := relocations at: self.

v := self calculateS: s A: a P: p.
v == AddressOutOfRange ifTrue: [
AcRelocationOverflow new
relocation: self;
signal: 'Relocation overflow'.
^ self
].

self fixup: memory at: p with: v
]

{ #category : #initialization }
AcRelocation >> initializeWithSymbol: symbolObj addend: addendObj [
self symbol: symbolObj.
self addend: addendObj.
]

{ #category : #testing }
AcRelocation >> isAcRelocation [
^ true
]

{ #category : #testing }
AcRelocation >> isValidForInstruction: insn [
^ self class isValidForInstruction: insn
]

{ #category : #copying }
AcRelocation >> postCopy [
symbol := symbol copy
]

{ #category : #accessing }
AcRelocation >> symbol [
^ symbol
]

{ #category : #accessing }
AcRelocation >> symbol: anObject [
self assert: (symbol isNil or: [ symbol = anObject ]).

symbol := anObject.
]
20 changes: 20 additions & 0 deletions src/ArchC-Core/AcRelocationFailed.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Class {
#name : #AcRelocationFailed,
#superclass : #Error,
#instVars : [
'relocation'
],
#category : #'ArchC-Core-Relocations'
}

{ #category : #accessing }
AcRelocationFailed >> relocation [
^ relocation
]

{ #category : #accessing }
AcRelocationFailed >> relocation: anAcRelocation [
self assert: anAcRelocation isAcRelocation.

relocation := anAcRelocation.
]
15 changes: 15 additions & 0 deletions src/ArchC-Core/AcRelocationOverflow.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class {
#name : #AcRelocationOverflow,
#superclass : #AcRelocationFailed,
#category : #'ArchC-Core-Relocations'
}

{ #category : #accessing }
AcRelocationOverflow >> isResumable [
^ self mayProceed
]

{ #category : #'default actions' }
AcRelocationOverflow >> mayProceed [
^ true
]
70 changes: 70 additions & 0 deletions src/ArchC-Core/AcRelocationQuery.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Class {
#name : #AcRelocationQuery,
#superclass : #Query,
#instVars : [
'relocation'
],
#category : #'ArchC-Core-Relocations'
}

{ #category : #queries }
AcRelocationQuery class >> isAbstract [
"Return if this class is an abstract class.
True is returned here for myself only; false for subclasses.
Abstract subclasses must redefine this again."

^ self == AcRelocationQuery.
]

{ #category : #queries }
AcRelocationQuery class >> query [
^ self shouldNotImplement. "Use #query: someRelocation"
]

{ #category : #queries }
AcRelocationQuery class >> query: anAcRelocation [
self assert: anAcRelocation isAcRelocation.

^ self new
relocation: anAcRelocation;
query
]

{ #category : #'default actions' }
AcRelocationQuery >> defaultResumeValue [
"If not handled, just throw AcRelocationFailure since we did not get
requested data (GOT / TOC entry, PLT and what not)."

AcRelocationFailed new
relocation: relocation;
signal: 'Relocation request failed!'.
^ nil
]

{ #category : #accessing }
AcRelocationQuery >> relocation [
^ relocation
]

{ #category : #accessing }
AcRelocationQuery >> relocation: anAcRelocation [
self assert: anAcRelocation isAcRelocation.

relocation := anAcRelocation.
]

{ #category : #'handler actions' }
AcRelocationQuery >> resume: symbolAndAddr [
"Respond to request with given symbol and its (final) address"

self assert:(symbolAndAddr isKindOf: Association).
self assert: symbolAndAddr key notNil.
self assert: symbolAndAddr value isInteger.

super resume: symbolAndAddr
]

{ #category : #accessing }
AcRelocationQuery >> symbol [
^ relocation symbol
]
Loading