From: Andrew Fish <afish@apple.com>
To: valerij zaporogeci <vlrzprgts@gmail.com>
Cc: edk2-devel <edk2-devel@lists.01.org>
Subject: Re: TE relocations
Date: Wed, 05 Oct 2016 13:58:33 -0700 [thread overview]
Message-ID: <85365CC4-AEB9-41F3-9F71-A32A0AFF4588@apple.com> (raw)
In-Reply-To: <CANPuzFyCvvux4cBW+CyfUp_CdunS9RN63tA5VUEd7SkuVpv0Og@mail.gmail.com>
> On Oct 5, 2016, at 12:24 PM, valerij zaporogeci <vlrzprgts@gmail.com> wrote:
>
> Thank you, Andrew, I guess I understood. PE/TE images get relocated at
> FV creation time, they execute directly from FV and they are linked at
> where they really will lie in XIP memory.
Pedantically speaking PE/TE images are not linked at FV creation time they are relocated to the XIP address. For the most part this is the same thing that EFI does when it loads the PE/TE image into malloced memory.
> And the alignment value
> chosen is not that minimum of 512 byte, mentioned in the PE
> specification. Now it's clear. The only thing I still don't get is
> that:
> Suppose you have your input PE image, which you need to translate in
> TE. It's linked at 0. It has a code section and it is lying (in the
> file) at the first available 20h aligned offset right after all the
> headers. Now you make translation into TE following the specification
> algorithm. It says you should strip away all not needed stuff from the
> beginning of the PE file, then put TE header instead (28h byte) and
> right after it you should place all the remaining PE content. But
> constructed this way TE, will break absolute referencies in the code.
> Suppose some code references some data, using its address: address =
> <ImageBase>:0 + <DataSectionBase> + <SectionOffset>. Where
> DataSectionBase is the offset of the beginning of the section from the
> ImageBase, thus its RVA. ImageBase remains the same - 0, SectionOffset
> too, but _actual_ DataSectionBase, would not be the same for the newly
> created TE, because stripping got the data section closer to the file
> beginning. Every reference to the data would be broken. Base
> relocation at the FV creation won't help, since it changes ImageBase
> (from 0 to "ROM address"), it doesn't fix DataSectionBase mismatch. If
> after PE->TE translation, "somehow" DataSectionBase remains the same,
> then what space saving it is? :)
I don't think you are Groking the simplicity inherent in the TE design. The image layout of a PE/COFF or TE image is the same.
>From low address to hight address:
IMAGE_HEADER
SECTION_TABLE
.text SECTION
.data SECTION
.reloc SECTION
.debug SECTION
The 1st section after the SECTION_TABLE is going to start on a section alignment boundary, and each subsequent section will start on the aligned boundary.
>From the PE/COFF point of view it is only the system that cares about the PE/COFF header not really the code that executes. So the image does not really care that it is TE or PE/COFF. I'll use P for PE/COFF header and T for TE header to make my point.
P
P
P
PT
----
SECTION_TABLE
---
.text SECTION
.data SECTION
.reloc SECTION
.debug SECTION
This layout is the basic reason that debuggers work even though they don't understand TE. When you tell a debugger the start location of a TE image you actually give the debugger the start of P. There is info in the TE header that lets you calculate the location that would have been the start of the PE/COFF header. Conceptually it is the space before the TE image (P but no T) that is being saved in the ROM by using the TE format.
Feel free to look at the code, https://github.com/tianocore/edk2/blob/master/MdePkg/Library/BasePeCoffLib/BasePeCoff.c <https://github.com/tianocore/edk2/blob/master/MdePkg/Library/BasePeCoffLib/BasePeCoff.c> , that decodes PE/COFF and TE.
Thanks,
Andrew Fish
PS It is a little tricky to actually look at TE images in the build output. All the modules get constructed as PE/COFF. If you search the Build/ directories for .te files these are a TE Section in the FV, thus they start with a 4 byte section header. If you remove the 1st 4 bytes you get a TE Image.
next prev parent reply other threads:[~2016-10-05 20:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-05 15:08 TE relocations valerij zaporogeci
2016-10-05 16:51 ` Andrew Fish
2016-10-05 19:24 ` valerij zaporogeci
2016-10-05 20:58 ` Andrew Fish [this message]
2016-10-05 21:45 ` valerij zaporogeci
2016-10-05 22:11 ` Andrew Fish
2016-10-05 23:25 ` valerij zaporogeci
2016-10-06 0:07 ` Andrew Fish
2016-10-06 13:01 ` valerij zaporogeci
2016-10-08 8:54 ` Gao, Liming
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=85365CC4-AEB9-41F3-9F71-A32A0AFF4588@apple.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox