1*b9411a12SAndroid Build Coastguard Worker# Relocatable Modules (rmodules) 2*b9411a12SAndroid Build Coastguard Worker 3*b9411a12SAndroid Build Coastguard WorkerRelocatable modules are currently only used on x86. Relocatable 4*b9411a12SAndroid Build Coastguard Workermodules are executables. Exectuables which can be executed anywhere in 5*b9411a12SAndroid Build Coastguard Workermemory. Anywhere means that the module does not need to be executed 6*b9411a12SAndroid Build Coastguard Workerat a defined memory address which is known at build/link time. For 7*b9411a12SAndroid Build Coastguard Workercoreboot stages like bootblock and romstage it is known at build 8*b9411a12SAndroid Build Coastguard Workertime at which addresses they are executed. For some exectuables it 9*b9411a12SAndroid Build Coastguard Workeris however not known at which specific address they are executed in 10*b9411a12SAndroid Build Coastguard Workerruntime (for example postcar and ramstage). Relocateable modules 11*b9411a12SAndroid Build Coastguard Workerusually allocate the space for the modules just before they are 12*b9411a12SAndroid Build Coastguard Workersupposed to be executed. After enough space is allocated, CBMEM will 13*b9411a12SAndroid Build Coastguard Workerreturn the location of the allocated space. Now the relocation can be 14*b9411a12SAndroid Build Coastguard Workerdone by fixing up all relocation entries in the relocatable module 15*b9411a12SAndroid Build Coastguard Workerbased on the location of the binary (which was returned by CBMEM 16*b9411a12SAndroid Build Coastguard Workerat runtime). 17*b9411a12SAndroid Build Coastguard Worker 18*b9411a12SAndroid Build Coastguard Worker# Implementation Details 19*b9411a12SAndroid Build Coastguard Worker 20*b9411a12SAndroid Build Coastguard Worker## build time 21*b9411a12SAndroid Build Coastguard Worker 22*b9411a12SAndroid Build Coastguard WorkerAt build time the rmodtool (util/cbfstool/rmodtool.c) is used to 23*b9411a12SAndroid Build Coastguard Workercreate relocatable modules. The rmodtool basically takes an ELF 24*b9411a12SAndroid Build Coastguard Workerfile as an input and writes an ELF as output. It basically does 25*b9411a12SAndroid Build Coastguard Workera simple conversion from one ELF file to another slighty changed 26*b9411a12SAndroid Build Coastguard WorkerELF file. First the tool makes sure that the ELF file fits a few 27*b9411a12SAndroid Build Coastguard Workerrequirements. For example there can only be one segment (loadable 28*b9411a12SAndroid Build Coastguard Workerprogram header) in the input ELF file. After that it goes through 29*b9411a12SAndroid Build Coastguard Workerthe ELF relocation table and takes any entry that applies to the one 30*b9411a12SAndroid Build Coastguard Workersegment we want to load at runtime. The rmodtool will then write all 31*b9411a12SAndroid Build Coastguard Workerthese relocation entires in a new ELF section called ".reloc". After 32*b9411a12SAndroid Build Coastguard Workerthat the ELF relocation table will be cleared. 33*b9411a12SAndroid Build Coastguard Worker 34*b9411a12SAndroid Build Coastguard WorkerOne can split the rmodules in two different kinds: 35*b9411a12SAndroid Build Coastguard Worker1. coreboot stages (postcar, ramstage) 36*b9411a12SAndroid Build Coastguard Worker2. simple binaries (smm, smmstub, sipi\_vector) 37*b9411a12SAndroid Build Coastguard Worker 38*b9411a12SAndroid Build Coastguard WorkerThey are actually handled the same by the build system and only differ 39*b9411a12SAndroid Build Coastguard Workerin the fact, that they are either coreboot stages or they are not. 40*b9411a12SAndroid Build Coastguard Worker 41*b9411a12SAndroid Build Coastguard WorkerIn the end the ELF files will have three different ELF sections, 42*b9411a12SAndroid Build Coastguard Workerwhich are all created by the rmodtool. 43*b9411a12SAndroid Build Coastguard Worker1. relocation header (.header) 44*b9411a12SAndroid Build Coastguard Worker2. program (.program) 45*b9411a12SAndroid Build Coastguard Worker3. relocation entries (.relocs) 46*b9411a12SAndroid Build Coastguard Worker 47*b9411a12SAndroid Build Coastguard Worker## runtime 48*b9411a12SAndroid Build Coastguard Worker 49*b9411a12SAndroid Build Coastguard WorkerEither rmodule\_load (lib/rmodule.c) is used directly or through the 50*b9411a12SAndroid Build Coastguard Workerrmodule\_stage\_load (lib/rmodule.c) wrapper. It is used to load the 51*b9411a12SAndroid Build Coastguard Workerstages (postcar and ramstage) or small programs like (sipi\_vector, 52*b9411a12SAndroid Build Coastguard Workersmm, smmstub) into memory before jumping to them. In the case of a 53*b9411a12SAndroid Build Coastguard Workercoreboot stage, CBMEM is used to allocate space for the stage in memory 54*b9411a12SAndroid Build Coastguard Workervia the rmodule\_cbfs\_allocater (lib/rmodule.c). At this point the 55*b9411a12SAndroid Build Coastguard Workerlocation of the stage in memory is known and all relocation (address 56*b9411a12SAndroid Build Coastguard Workerfixups) need to be done now. This is basically just a simple loop that 57*b9411a12SAndroid Build Coastguard Workergoes through each relocation entry. Each relocation entry is just an 58*b9411a12SAndroid Build Coastguard Workeraddress pointing to a location that needs relocation. The relocation 59*b9411a12SAndroid Build Coastguard Workeritself is just a simple addition, that adds an offset from where the 60*b9411a12SAndroid Build Coastguard Workerimage was "supposed" to be at link time, to where it is now relocated. 61*b9411a12SAndroid Build Coastguard Worker 62*b9411a12SAndroid Build Coastguard Worker## module\_parameters 63*b9411a12SAndroid Build Coastguard Worker 64*b9411a12SAndroid Build Coastguard Workermodule\_parameters is a section inside the rmodule ELF file. Its 65*b9411a12SAndroid Build Coastguard Workerbasically a way to pass runtime information to an rmodule 66*b9411a12SAndroid Build Coastguard Workerbefore jumping to it. The caller will use rmodule\_parameters() 67*b9411a12SAndroid Build Coastguard Worker(lib/rmodule.c) to get the runtime address of the module\_parameters 68*b9411a12SAndroid Build Coastguard Workerand the callee (the rmodule itself) usually appends the section to 69*b9411a12SAndroid Build Coastguard Workerspecific types via compiler attributes. For example: 70*b9411a12SAndroid Build Coastguard Worker``` 71*b9411a12SAndroid Build Coastguard Workerstatic const 72*b9411a12SAndroid Build Coastguard Workervolatile __attribute((aligned(4), __section__(".module_parameters"))) 73*b9411a12SAndroid Build Coastguard Workerstruct smm_runtime smm_runtime; 74*b9411a12SAndroid Build Coastguard Worker``` 75*b9411a12SAndroid Build Coastguard Worker 76*b9411a12SAndroid Build Coastguard Worker# x86 why rmodules 77*b9411a12SAndroid Build Coastguard Worker//TODO 78*b9411a12SAndroid Build Coastguard Workerx86: postcar and ramstage cannot conflict with payload regarding 79*b9411a12SAndroid Build Coastguard Workermemory placement. Therefore payload location is usually fixed and 80*b9411a12SAndroid Build Coastguard Workerpostcar/ramstage can be placed at a location in memory that is 81*b9411a12SAndroid Build Coastguard Workerfigured out at runtime. 82