Commit Graph

156 Commits

Author SHA1 Message Date
sergei
1f8a67f552 + Property TObjSymbol.ObjData, allows to access owning TObjData for external/common symbols, i.e. ones with ObjSection=nil.
* For common symbols, report objdata of the original ObjSymbol, not of the resolving one, so the map file shows where it comes from.
+ TElfExeOutput.OrderOrphanSections method, handles object sections not mentioned in script similar to ld.
- t_linux.pas: removed sections that are not part of ld scripts, they are now handled by OrderOrphanSections.

git-svn-id: trunk@25184 -
2013-07-30 09:23:48 +00:00
sergei
5b6e50c9da * It is incorrect to use var_align() in linker code: it depends on current_settings.alignment, which is valid only while compiling, not while linking. Replaced with size_2_align().
git-svn-id: trunk@25182 -
2013-07-29 15:51:30 +00:00
sergei
1fea5a82c1 * Replaced oso_disabled flag by TExeSection.Disabled boolean property. Makes code a bit nicer to read.
git-svn-id: trunk@24361 -
2013-04-28 20:29:49 +00:00
sergei
b86372ccec + Internal linker: process unreferenced .pdata sections on x86_64-win64, restores the possibility to use GAS on win64 (which got broken by introducing SEH).
git-svn-id: trunk@24235 -
2013-04-13 18:52:50 +00:00
sergei
c3f8086598 + Internal linker: base support for sections that aren't referenced but must be linked if section referenced by them is linked (like win64 .pdata). Actual handling is still to be implemented in descendant classes.
git-svn-id: trunk@24224 -
2013-04-11 17:42:09 +00:00
sergei
999c3a0a02 * COFF linker: postpone import section generation until unreferenced sections (and imports) are removed, and put all remaining imports into a single section. This is much more efficient than generating multiple smart-linkable import sections and immediately discarding a good half of them.
git-svn-id: trunk@24123 -
2013-04-02 05:46:32 +00:00
sergei
cded05ccef + MIPS linker: support linking PIC object files with non-PIC ones, by routing absolute calls into PIC code through stubs that load R25.
git-svn-id: trunk@24098 -
2013-03-31 19:58:52 +00:00
sergei
1d32b107f5 * Made address comparison 64-bit safe.
* Referencing a section is basically referencing its AT_SECTION symbol, fixed reporting in map file.

git-svn-id: trunk@24029 -
2013-03-28 10:44:09 +00:00
florian
20a534e63f * fixed code which causes range check errors when compiled with -Cr
git-svn-id: trunk@23889 -
2013-03-17 14:24:50 +00:00
sergei
6484911f05 * TExeOutput.FixupRelocations method made virtual.
* TElfExeOutput.MemPos_Start split into four smaller methods.

git-svn-id: trunk@23684 -
2013-03-03 20:01:54 +00:00
sergei
f98e09d86e - TObjData.sectiontype2options: removed oso_keep flags. They don't have any effect here because this function is used only during assembling, and oso_keep cannot be represented in object files and therefore gets lost before linking.
git-svn-id: trunk@23364 -
2013-01-11 17:14:44 +00:00
sergei
2bba4e2d44 + Stabs debug info needs swapping when linking for different endianness.
* A few minor tweaks for Stabs generation.

git-svn-id: trunk@23316 -
2013-01-05 12:26:23 +00:00
sergei
d7c5daeccd * Don't calculate data position of each object section. Instead, write out a copy of exesection's memory image, i.e. every objsection is placed at the same offset from its owning exesection both on disk and in memory. This approach is simpler and works regardless of alignment requirements, while existing one could work only if SectionDataAlign<=SectionMemAlign.
* Netware target may be broken by this change; at least it deserves a good cleanup because it contains a lot of code trying to solve similar task: to write exesections without alignment and deal with objsections that are nevertheless being aligned by TExeOutput.

git-svn-id: trunk@23291 -
2013-01-02 10:10:23 +00:00
sergei
00ca4a74a3 * Reworked/fixed TExeSymbol.State behavior:
* Added symstate_undefweak for undefined symbols having only weak references to them, this state is removed when a normal reference to symbol is seen.
  * Use a separate boolean property to determine if ExeSymbol is referenced.
  * Use a separate property TExeOutput.AllowUndefinedSymbols to suppress error messages when linking dynamic shared objects.
  * Don't issue InternalError when an undefined symbol is encountered during unused section removal.

git-svn-id: trunk@23127 -
2012-12-09 08:02:58 +00:00
sergei
36859068f5 * Propagate AS_NEEDED flag further into Load_DynamicObject and handle it properly: shared objects marked with AS_NEEDED are added to dependencies only if they actually resolve some symbol.
* Dropped TObjInput.newObjData method. Instead, TObjData instances are created directly in ReadObjData, this gives possibility to create a different type of TObjData based on file header.
- Dropped TObjInput destructor, it does nothing except calling inherited.

git-svn-id: trunk@23118 -
2012-12-06 18:19:19 +00:00
sergei
129d737523 + Allow TObjRelocation to be created without a symbol, such relocations are being used to tag specific positions on non-x86 targets.
* Write relocation format dependent dynamic tags based on actual sh_type of dynamic relocation sections, instead of global relocs_use_addend flag.
* Don't write DT_REL[A]COUNT tag if .rel[a].dyn section is not present.

git-svn-id: trunk@23083 -
2012-12-01 13:38:28 +00:00
sergei
0369a30f49 * Internal linker: made sec_rodata_norel read-only.
* For Win32 and Win64 targets made sec_rodata read-only, too (for these targets it doesn't matter if read-only section contains relocations).

git-svn-id: trunk@23025 -
2012-11-18 17:40:17 +00:00
sergei
1aea22a6ca + Internal linker: generic and ELF-specific support for grouped sections, allow same symbol to be defined in several COMDAT groups.
* ELF linker: don't make unresolved weak symbols dynamic if linking statically (-Xt in command line).
* Now internal linker produces working executable for tw14265 also on i386-linux when compiling manually (still unable to locate required libraries when run from test suite).

git-svn-id: trunk@22987 -
2012-11-15 13:41:25 +00:00
sergei
d019670495 - Removed generic implementation of TObjData.sectionname, its existence is useless because it is completely overridden by all TObjData descendants.
- Removed separate ELF section names for PIC. The only difference was .data named .data.rel; however .rel suffix has nothing to do with PIC. It only signifies that the section has relocations so such sections can be grouped together in output file and reduce number of pages for dynamic linker to visit  while resolving the relocations at load time. At the same time, no existing link scripts distinguish between .rel and any other suffix (except .rel.ro, but it's a different story), meaning that long section names will break .rel grouping.
While support for .data.rel can be added similar to existing rodata handling (separate sec_data and sec_data_norel sections), this doesn't seem worth the trouble.

git-svn-id: trunk@22915 -
2012-11-02 08:03:54 +00:00
sergei
8a56c9f7dc * TExeOutput.Order_Symbol and Order_ProvideSymbol: Instead of searching objsection by name, search for the symbol and use its objsection.
* TExeOutput.WriteExeSectionContent: To write proper gap between exe sections, don't align writer position. The desired position is present in exesection.DataPos, so pad right up to it.

git-svn-id: trunk@22881 -
2012-10-30 12:49:15 +00:00
sergei
499162ca50 * Internal linker: fixed alignment routines. Neither of existing ones is suitable for 64-bit targets: cutils.align() is 32-bit only and moreover signed, system.align() crashes on zero alignment values and handles only pointer size of source platform.
Therefore, added dedicated routines align_aword and align_qword that handle target platform size and 64-bit unsigned, respectively.
+ Also added TExeOutput.FixedSectionAlign boolean that, when set to False, ignores SectionDataAlign and SectionMemAlign and aligns every exe section to its own SecAlign value. This kind of alignment is used on ELF targets.

git-svn-id: trunk@22858 -
2012-10-27 14:21:14 +00:00
sergei
ee62a1ed50 * Fixed unclosed comment from r22775.
git-svn-id: trunk@22777 -
2012-10-19 18:21:43 +00:00
sergei
6d70009f06 + ELF linker. Works on x86_64-linux and i386-linux good enough to pass the testsuite, but still requires a lot of work in nearly all aspects. In particular, no attempt to resolve symbols from shared libs is done, everything is just treated as imports. Symbol versioning isn't supported either.
x86_64 is the most elaborated, has some degree of indirect function (GNU_IFUNC) and TLS support, so it is even able to link with static libc/pthreads code (tw14265) and produce an executable that can launch (but still fails due to invalid DWARF unwind info).

i386 produces working shared libraries if they are compiled with -Cg, without one your mileage may vary. tw14265 does not link yet due to missing COMDAT group support.

git-svn-id: trunk@22775 -
2012-10-19 17:21:08 +00:00
sergei
a178e28275 + Extend TObjRelocation to hold untranslated platform-specific relocation types. This allows to avoid translating relocations into generic format when reading object files (which is just waste of time), and allows TObjRelocationType enumeration contain only types that are actually produced by compiler.
git-svn-id: trunk@22667 -
2012-10-16 10:40:34 +00:00
sergei
3d63b1a2b2 + Added a flag allowing a section to be written into both .dbg and executable files. This is needed at least for ELF SHT_NOTE sections.
git-svn-id: trunk@22614 -
2012-10-11 19:26:40 +00:00
sergei
fde944bf5d + Declare AT_TLS and AT_GNU_IFUNC symbol types, these are needed to handle object files created by gcc in ELF internal linker.
* TExeOutput.FixupSymbols:
  + Collect AT_GNU_IFUNC symbols in IndirectObjSymbols list for further processing by ELF back-end.
  * Do not overwrite bind and size while fixing AB_COMMON symbols, this removes need in storing symbol size in every relocation and dedicated handling of oso_common section.
  * Remove symbols with objsection<>nil from lists, so calling FixupSymbols several times skips already processed symbols.

git-svn-id: trunk@22393 -
2012-09-14 17:22:48 +00:00
sergei
c41b407119 * Changed TObjSection.WriteStr method to write a zero byte after the string.
+ Added TObjSection.WriteBytes method that writes just a string (without zero byte).
* ogelf.pas: Fixed st_value of COMMON symbols, it must contain required symbol alignment.
+ ogelf.pas: Some more definitions.

git-svn-id: trunk@22332 -
2012-09-06 06:06:04 +00:00
sergei
b1b175dacc + Internal linker support for weak symbols.
+ Allow unresolved external symbols in RemoveUnreferencedSections, such symbols are marked as 'dynamic' if referenced. This approach allows to collect unused sections early and generate import structures only for actually used symbols.

git-svn-id: trunk@22312 -
2012-09-04 13:16:56 +00:00
sergei
6f4e8927cd * Specify explicitly whether properties of an ObjSection should be considered when adding it to ExeSection, instead of deciding it implicitly.
git-svn-id: trunk@22292 -
2012-09-03 09:12:50 +00:00
sergei
d79511f96e + Basic ld script parsing capabilities (barely enough to parse glibc2.1 'libc.so' files, lacks any error handling).
+ Support linker input source grouping functionality.
* Promote TStaticLibrary to something like generic linker input statement, it can now hold regular ObjData or a group of other TStaticLibrary objects in addition to tarobjectreader.

git-svn-id: trunk@22155 -
2012-08-21 08:37:39 +00:00
sergei
ec56677927 + Added method TObjSection.writeReloc_internal to write relocations to given section+offset without need to have a symbol at target location.
* Optimized COFF importing code using the new method.
  - Don't create .text sections for imported variables.
  - Don't create .idata$6 sections for imports by ordinal.

git-svn-id: trunk@22086 -
2012-08-15 12:22:07 +00:00
sergei
c1e7e9c85e * TObjData.symbolref: don't lose the weak binding of asm symbol. This fixes tests/tweaklib*.pp at least for Linux x86 targets with internal assembler.
* Enabled weak linking for Linux targets.

git-svn-id: trunk@21998 -
2012-08-02 12:15:55 +00:00
sergei
1d09005542 + Implemented two-stage removal of empty exe sections. Candidates for removal are first marked with oso_disabled flag, then actually removed. Descendants of TExeOutput that override MemPos_Start may modify list of sections pending removal. In particular, the COFF-specific .reloc section no longer has to be handled in base TExeOutput class.
git-svn-id: trunk@21971 -
2012-07-26 10:04:12 +00:00
sergei
31d004e056 + Basic executable stack support in TExeOutput.
+ Two stubs (TExeOutput.Load_DynamicObject and TObjInput.CanReadObjData) to override in ELF linker.
+ Stop linking if errors were detected while loading object files.
* Changed TStaticLibrary to TObject. It is never looked up by name, so hashing is redundant; moreover its name has been changed to TCmdStr, which may be trimmed by TFPHashList. 

git-svn-id: trunk@21968 -
2012-07-25 11:08:54 +00:00
sergei
24c7b7f433 * If alignment requirement of data is bigger than alignment of the section being written to, increase section alignment to match. This mostly affects cases of non-smart linking, where wrong initial alignment used to cause hard to catch misalignment issues. It also largely obsoletes the need to supply the initial section alignment (hidden tai_section constructor,etc).
* Likewise, when merging object sections into exe sections, set alignment of exe section to maximum value.

git-svn-id: trunk@21964 -
2012-07-24 08:38:11 +00:00
sergei
06844fdb23 * Entirely moved relocation fixup functionality from TObjSection to TExeOutput.
git-svn-id: trunk@21944 -
2012-07-22 07:21:11 +00:00
sergei
2bc792fe97 * TObjData.FName made protected.
* Allow EntryName to remain unspecified.

git-svn-id: trunk@21940 -
2012-07-20 12:50:21 +00:00
sergei
3b3da49ad6 * Merged TElfObjSection.secshidx and TCoffObjSection.secidx into TObjSection.index.
git-svn-id: trunk@21848 -
2012-07-10 13:34:55 +00:00
sergei
712f5d1c26 - Removed oso_readonly and oso_noload section options, it is enough to have just oso_write and oso_load to express possible section states.
git-svn-id: trunk@21825 -
2012-07-09 12:58:37 +00:00
sergei
1c5fa5448e * Do not dereference null pointer, 'real' (not script-provided) common symbols don't have objsection assigned at this point.
git-svn-id: trunk@21748 -
2012-07-01 13:42:25 +00:00
sergei
65e701a198 * Fixed operation of 'PROVIDE' link script directive (all symbols were incorrectly put into .bss section, instead of locations given by placement of the directive).
* Changed type of symbols created by link script from AT_FUNCTION to AT_DATA. This isn't significant for COFF, but matters for ELF targets.
* Changed alignment of .gnu_debuglink section from 0 to 1, this matches behavior of ld.

git-svn-id: trunk@21741 -
2012-06-30 17:32:45 +00:00
sergei
c65abdeeae * Implemented link map generation without involving TObjSection.ObjSymbolDefines, and removed the latter because it isn't used otherwise. The new approach uses CPU/memory only when map generation is requested.
git-svn-id: trunk@21737 -
2012-06-30 07:51:45 +00:00
sergei
80bbd388f9 * Made removal of unused ExeSymbols a separate procedure, call it earlier and regardless of presence of the 'SYMBOLS' directive in the linker script.
git-svn-id: trunk@21727 -
2012-06-27 20:26:49 +00:00
sergei
8c8883cc35 * Once TExeOutput is accessible in TObjSection.fixuprelocs, its ImageBase can be used directly, without a need to have a copy in every TObjSection.
* Also simplified COFF symbol loading, separate array of symbol sizes is not needed because these sizes never change during the load.

git-svn-id: trunk@21704 -
2012-06-25 18:39:46 +00:00
sergei
bd7ebdce18 * ELF relocation cleanup/improvement:
+ Store size of relocation and explicit addend in TObjRelocation (reusing 'orgsize' field for the latter). This removes need for reading addends back from section data, addends are stored in full 64 bits and therefore not truncated.
  + Relocation style is now controlled by relocs_use_addend variable instead of $ifdef's.
  - Removed (never working) checks forbidding relocations of readonly sections. At the linking stage readonly sections *can* have relocations, executable stage is different matter to be handled elsewhere.
  - removed ugly hack with mapping 32-bit absolute relocations to RELOC_RVA.
  + support 64-bit relative relocations.
  * actualized list of x86_64 relocations.

git-svn-id: trunk@21662 -
2012-06-20 14:16:48 +00:00
sergei
98fffb7981 * Overloaded MemPos_ExeSection and DataPos_ExeSection accepting a TExeSection instead of name.
git-svn-id: trunk@21661 -
2012-06-20 13:33:18 +00:00
sergei
7cda919547 * Pass ExeOutput as argument to TObjSection.fixuprelocs, needed for ELF targets to relocate the complex GOT stuff.
* TExeSection.AddObjSection made virtual.
- TExeSymbol.ExeSection is nowhere used, removed.

git-svn-id: trunk@21626 -
2012-06-16 22:33:26 +00:00
sergei
807a2f243f * Fixed control flow after r21492, was causing IE 200602252 if compiling with "-g -Xg" (Mantis #22224)
- Also removed accidentally committed debug code.

git-svn-id: trunk@21522 -
2012-06-07 22:30:50 +00:00
sergei
7d3294b504 * Moved 4 procedures for basic reading/writing TObjSection contents into ogcoff.pas, so they don't have to be reimplemented for every output format.
git-svn-id: trunk@21492 -
2012-06-05 19:52:40 +00:00
sergei
8814f56081 * Fixed oso_debug section attribute being not preserved while reading/writing DJCOFF object files.
* Fixed header data positions for DJCOFF executables, they need adjustment by stub size.
* Fixed symbol values in DJCOFF executables, they must be absolute.
* Fixed missing oso_common flag on TExeOutput.commonObjSection, causing incorrect COFF relocations to this section.

git-svn-id: trunk@21408 -
2012-05-28 11:46:52 +00:00