Commit graph

71 commits

Author SHA1 Message Date
Warner Losh d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Warner Losh b3e7694832 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:16 -06:00
John Baldwin e5c4737955 amd64 crt1: Explicitly use a PLT entry for main in the PIC case.
This ensures GNU as generates a R_X86_64_PLT32 relocation instead of
R_X86_64_32.

Reviewed by:	kib
Fixes:		c969310c99 csu: Implement _start using as to satisfy unwinders on x86_64
Differential Revision:	https://reviews.freebsd.org/D41101
2023-07-19 11:53:33 -07:00
Dmitry Chagin f049c92ad6 csu: Add the prologue and epilogue to the _init and _fini on x86_64
Normally, modern unwinders uses Dwarf information to unwind stack,
however in case when the code is not annotated by Dwarf instructions,
unwinders fallbacks to a frame-pointer based algorithm.

That is allows libunwind to unwind stack from global constructors and
destructors. Also it makes gdb happy as it printed nonexistent frame
before.

Reviewed by:		kib
Differential Revision:	https://reviews.freebsd.org/D40795
2023-07-01 10:52:10 +03:00
Dmitry Chagin c969310c99 csu: Implement _start using as to satisfy unwinders on x86_64
The right unwinding stop indicator should be CFI-undefined PC.
https://dwarfstd.org/doc/Dwarf3.pdf - page 118:
If a Return Address register is defined in the virtual unwind table,
and its rule is undefined (for example, by DW_CFA_undefined), then
there is no return address and no call address, and the virtual
unwind of stack activations is complete.

This requires the crt code be built with unwind tables, for that remove
-fno-asynchronous-unwind-tables to enable unwind tables generation.

PR:			241562, 246322, 246537
Reviewed by:		kib
Differential Revision:	https://reviews.freebsd.org/D40780
2023-06-29 19:34:39 +03:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix
2023-05-12 10:44:03 -06:00
Konstantin Belousov 51015e6d0f csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.

This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.

Reviewed by:	markj
Discussed with:	jrtc27 (some objections, see the review), imp
Tested by:	markj (aarch64)
Sponsored by:	The FreeBSD Foundation
MFC after:	3 weeks
Differential revision:	https://reviews.freebsd.org/D37220
2023-03-12 00:50:03 +02:00
Gleb Popov feac06569a lib/csu: Get rid of unused fptr typedef on amd64.
Make i386 and riscv parts not use it.

Approved by: kib

Differential Revision: https://reviews.freebsd.org/D31281
2021-07-25 13:51:09 +03:00
John Baldwin b0ee263dbd Consolidate duplicated logic in csu Makefiles to lib/csu/Makefile.inc.
Reviewed by:	kib
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D25537
2020-07-03 00:09:41 +00:00
John Baldwin 99282790b7 Remove the sed hack for ABI tag notes.
The ELF notes compiled in C were placed in a section with the wrong type
(SHT_PROGBITS instead of SHT_NOTE).  Previously, sed was used on the
generated assembly to rewrite the section type.  Instead, write the notes
in assembly which permits setting the correct section type directly.

While here, move inline assembly entry points out of C and into assembly
for aarch64, arm, and riscv.

Reviewed by:	kib
Tested on:	amd64 (cirrus-ci), riscv64
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D25211
2020-06-15 19:38:48 +00:00
Ed Maste b18e62b6da csu: use BSD-1-clause license on csu files
Copyright on these files is held by kib@ and/or the Foundation, and both
agree to this change.

Approved by:	kib
2018-11-20 21:04:20 +00:00
Andrew Turner 31d62a73c2 Implement a BSD licensed crtbegin/crtend
These are needed for .ctors/.dtors and .jcr handling. The former needs
all the function pointers to be called in the correct order from the
.init/.fini section. The latter just needs to call a gcj specific function
if it exists with a pointer to the start of the .jcr section.

This is currently disabled until __dso_handle support is added.

Reviewed by:	emaste
MFC after:	1 month
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D17587
2018-10-25 17:39:41 +00:00
Konstantin Belousov bdafb02fcb Process irelocs for statically linked binaries from crt1 on x86.
This makes statically linked binaries with ifuncs operational.

Reported and tested by:	mjg
Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
Approved by:	re (rgrimes)
Differential revision:	https://reviews.freebsd.org/D17363
2018-10-13 23:52:55 +00:00
Pedro F. Giffuni 5e53a4f90f lib: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-26 02:00:33 +00:00
John Baldwin 5944f899a2 Rework r234502 to include a modified CFLAGS along with ACFLAGS.
On most architectures crt objects are compiled in a multiple-step process
so that sed can be run on the generated assembly.  As the final step,
the C compiler generates an object file from the modified assembly output.
Currently this last step uses $CC with only $ACFLAGS.  However, for other
uses in the tree, $ACFLAGS is meant to include assembly-specific compiler
flags that are in addition to $CFLAGS (see default .S.o rules
bsd.suffixes.mk).  In particular, external toolchains may require
additional flags to select a non-default target which will be present
in CFLAGS but not ACFLAGS.  To support this while still mitigating the
issue with CFLAGS described in r234502, include a modified CFLAGS that
excludes "-g" when assembling the modified assembly files.

Note that normally an assembler ($AS) is used to assemble .s flags to
object files (see bsd.suffixes.mk).  However, llvm-based toolchains do
not currently have a stand-alone assembler.

Reviewed by:	imp
Sponsored by:	DARPA / AFRL
Differential Revision:	https://reviews.freebsd.org/D10085
2017-04-07 19:53:14 +00:00
Enji Cooper 23f6875a43 Use SRCTOP-relative paths and .CURDIR with :H instead of ".." specified paths
This implifies pathing in make/displayed output

MFC after:    3 weeks
Sponsored by: Dell EMC Isilon
2017-01-20 03:52:16 +00:00
Ed Maste c6a33c8e88 Remove historical GNUC test
The requirement is for a GCC-compatible compiler and not necessarily
GCC itself. However, we currently expect any compiler used for building
the whole of FreeBSD to be GCC-compatible and many things will break if
not; there's no longer a need to have an explicit test for this in csu.

Sponsored by:	The FreeBSD Foundation
2015-12-08 19:32:58 +00:00
Bryan Drewery 7c6cc9de3a Fix installation of 32bit libraries after r288074.
FILES is not used when LIBRARIES_ONLY is set, which is used to build and
install the lib32 sysroot.  All of the csu files do quality as "libraries"
for this case so just undefine LIBRARIES_ONLY.

This is still better than the previous realinstall handling as it does
not hook into META_MODE properly.

Sponsored by:	EMC / Isilon Storage Division
2015-09-21 21:44:02 +00:00
Bryan Drewery 5ca34122ec Replace realinstall: and META_MODE staging hacks with FILES mechanism.
This partially reverts r270170 for lib/csu/i386 while retaining the
change for using bsd.lib.mk.

These FILES groups could go into lib/csu/Makefile.inc but I've kept them
in the Makefiles for clarity.

Sponsored by:	EMC / Isilon Storage Division
2015-09-21 18:39:13 +00:00
Simon J. Gerraty ccfb965433 Add META_MODE support.
Off by default, build behaves normally.
WITH_META_MODE we get auto objdir creation, the ability to
start build from anywhere in the tree.

Still need to add real targets under targets/ to build packages.

Differential Revision:       D2796
Reviewed by: brooks imp
2015-06-13 19:20:56 +00:00
Simon J. Gerraty 44d314f704 dirdeps.mk now sets DEP_RELDIR 2015-06-08 23:35:17 +00:00
Simon J. Gerraty 6e7d005c83 Put lib/csu/amd64/Makefile back the way it is in head
and handle staging via ../Makefile.inc
2014-11-30 18:42:29 +00:00
Simon J. Gerraty 9268022b74 Merge from head@274682 2014-11-19 01:07:58 +00:00
Bryan Drewery 5608fd23c2 Revert r267233 for now. PIE support needs to be reworked.
1. 50+% of NO_PIE use is fixed by adding -fPIC to INTERNALLIB and other
   build-only utility libraries.
2. Another 40% is fixed by generating _pic.a variants of various libraries.
3. Some of the NO_PIE use is a bit absurd as it is disabling PIE (and ASLR)
   where it never would work anyhow, such as csu or loader. This suggests
   there may be better ways of adding support to the tree. Many of these
   cases can be fixed such that -fPIE will work but there is really no
   reason to have it in those cases.
4. Some of the uses are working around hacks done to some Makefiles that are
   really building libraries but have been using bsd.prog.mk because the code
   is cleaner. Had they been using bsd.lib.mk then NO_PIE would not have
   been needed.

We likely do want to enable PIE by default (opt-out) for non-tree consumers
(such as ports). For in-tree though we probably want to only enable PIE
(opt-in) for common attack targets such as remote service daemons and setuid
utilities. This is also a great performance compromise since ASLR is expected
to reduce performance. As such it does not make sense to enable it in all
utilities such as ls(1) that have little benefit to having it enabled.

Reported by:	kib
2014-08-19 15:04:32 +00:00
Simon J. Gerraty ee7b0571c2 Merge head from 7/28 2014-08-19 06:50:54 +00:00
Bryan Drewery 864c53ead8 In preparation for ASLR [1] support add WITH_PIE to support building with -fPIE.
This is currently an opt-in build flag. Once ASLR support is ready and stable
it should changed to opt-out and be enabled by default along with ASLR.

Each application Makefile uses opt-out to ensure that ASLR will be enabled by
default in new directories when the system is compiled with PIE/ASLR. [2]

Mark known build failures as NO_PIE for now.

The only known runtime failure was rtld.

[1] http://www.bsdcan.org/2014/schedule/events/452.en.html
Submitted by:		Shawn Webb <lattera@gmail.com>
Discussed between:	des@ and Shawn Webb [2]
2014-06-08 17:29:31 +00:00
Simon J. Gerraty f5f7c05209 Updated dependencies 2013-02-16 01:23:54 +00:00
David E. O'Brien d9a447559b Sync with HEAD. 2013-02-08 16:10:16 +00:00
Konstantin Belousov d549ead6a9 Only assign the environ in the startup code when environ is NULL.
Preloaded library could have changed the environment, and
unconditional assingment to the environ undoes the customization.
The binaries needs to be recompiled to get the fix.

Move the common code to set up environ and __progname into the helper.
Note that ia64 possibly not fixed, due to it still using old csu.

Reported and tested by:	John Hein <jhein@symmetricom.com>
Reviewed by:	kan, scf
Approved by:	secteam (simon)
MFC after:	2 weeks
2013-01-07 17:58:27 +00:00
Simon J. Gerraty 23090366f7 Sync from head 2012-11-04 02:52:03 +00:00
Dimitry Andric d2cc835cb5 After r217375, some startup objects under lib/csu are built in a special
way: first they are compiled to assembly, then some sed'ing is done on
the assembly, and lastly the assembly is compiled to an object file.

This last step is done using ${CC}, and not ${AS}, because when the
compiler is clang, it outputs directives that are too advanced for our
old gas.  So we use clang's integrated assembler instead.  (When the
compiler is gcc, it just calls gas, and nothing is different, except one
extra fork.)

However, in the .s to .o rules in lib/csu/$ARCH/Makefile, I still passed
CFLAGS to the compiler, instead of ACFLAGS, which are specifically for
compiling .s files.

In case you are using '-g' for debug info anywhere in your CFLAGS, it
causes the .s files to already contain debug information in the assembly
itself.  In the next step, the .s files are also compiled using '-g',
and if the compiler is clang, it complains: "error: input can't have
.file dwarf directives when -g is used to generate dwarf debug info for
assembly code".

Fix this by using ${ACFLAGS} for compiling the .s files instead.

Reported by:	jasone
MFC after:	1 week
2012-04-20 21:00:39 +00:00
Konstantin Belousov a22748dbd9 Stop calling _init/_fini methods from crt1 for dynamic binaries. Do
call preinit, init and fini arrays methods from crt1 for static binaries.

Mark new crt1 with FreeBSD-specific ELF note.

Move some common crt1 code into new MI file ignore_init.c, to reduce
duplication.  Also, conservatively adjust nearby sources for style.

Reviewed by:	kan
Tested by:	andrew (arm), flo (sparc64)
MFC after:	3 weeks
2012-03-11 20:04:09 +00:00
Dimitry Andric 9ef4e3afcb Apply a workaround for a binutils issue with the .note.ABI-tag section
generated from lib/csu/common/crtbrand.c (which ultimately ends up in
executables and shared libraries, via crt1.o, gcrt1.o or Scrt1.o).

For all arches except sparc, gcc emits the section directive for the
abitag struct in crtbrand.c with a PROGBITS type.  However, newer
versions of binutils (after 2.16.90) require the section to be of NOTE
type, to guarantee that the .note.ABI-tag section correctly ends up in
the first page of the final executable.

Unfortunately, there is no clean way to tell gcc to use another section
type, so crtbrand.c (or the C files that include it) must be compiled in
multiple steps:

- Compile the .c file to a .s file.
- Edit the .s file to change the 'progbits' type to 'note', for the section
  directive that defines the .note.ABI-tag section.
- Compile the .s file to an object file.

These steps are done in the invididual Makefiles for each applicable arch.

Reviewed by:	kib
2011-01-13 20:44:31 +00:00
Konstantin Belousov 88c23d858b Add section .note.GNU-stack for assembly files used by 386 and amd64. 2011-01-07 16:07:51 +00:00
Dimitry Andric cbbcfbf8b7 Let all .c and .S files under lib/csu consistently use the __FBSDID()
macro for identification, instead of several different hand-rolled
variants (plain .ident, .ascii, etc).
2010-12-09 21:31:21 +00:00
Ulrich Spörlein 98b77738b8 Properly declare non-extern functions in crt1
Also move the declarations after __progname consistently to
make the distinction clearer.

Reviewed by:	jmallett
Approved by:	ed (co-mentor)
2010-03-05 13:28:05 +00:00
Konstantin Belousov c09ba32715 Properly support -fPIE by linking PIE binaries with specially-built
Scrt1.o instead of crt1.o, since the later is built as non-PIC.

Separate i386-elf crt1.c into the pure assembler part and C code,
supplying all data extracted by assembler stub as explicit parameters [1].
Hide and localize _start1 symbol used as an interface between asm and
C code.

In collaboration with:	kan
Inspired by:	PR i386/127387 [1]
Prodded and tested by:	rdivacky [1]
MFC after:	3 weeks
2009-12-02 16:34:20 +00:00
Konstantin Belousov af8d325c77 Add -fno-omit-frame-pointer to CFLAGS used to compile crt1.c on amd64.
For gcc' __builtin_frame_address() to work, all call frames need to save
frame pointer. In particular, this is important for the upper frame that
should terminate the chain.

No objections from:	jhb
PR:	amd64/126543
MFC after:	1 week
2008-08-22 09:23:39 +00:00
Bruce Evans d31f7e4991 Fixed profiling of main() for amd64 and i386. This started rotting
in 1993 in rev.1.5 of the i386 a.out version (csu/i386/crt0.c).
Profiling uses a magic label "eprol" to delimit the start of the part
of the text section covered by profiling.  This label must be placed
before the call to main() to get main() properly profiled.  It was
placed there in rev.1.1 of crt0.c.  Rev.1.5 imported the initial
implementation of shared libraries in FreeBSD and misplaced the label.
Fortunately, the misplaced label was misspelled and the old label
wasn't removed, so the new label had no effect.  Unfortunately, when
profiling was implemented for the ELF in 1998 in rev.1.2 of
csu/i386-elf/crt1.c, only the incorrectly placed label was copied
(after fixing its name).  The bug was then copied to all other arches.
The label seems to be still misplaced in NetBSD for most arches.  It
is in common.c for most arches so it is even further from being inside
the function that calls main().

I think "eprol" is short for "end of prologue", but it must be placed
before the end of the prologue so that it covers main().  crt0.c has
it before the calls atexit(_mcleanup) and monstartup(...), but it
cannot affect these calls so I moved it after the call to monstartup().
It now also covers the call to _init() but not the newer call to
_init_tls().  Profiling of _init() seems to be harmless, and the call
to _init_tls() seems to be misplaced.

Reviewed by:	jdp (long ago, for a slightly different i386 version)
2005-10-07 22:13:17 +00:00
Doug Rabson ccd13c49b5 Add support for TLS in statically linked programs. 2004-08-15 16:18:52 +00:00
Peter Wemm c50be14baa Adjust stack alignment so that when the 'call xxx' functions are
gathered into the middle of the _init and _fini sections, they get
executed with their expected stack alignment.
2004-03-21 01:39:01 +00:00
Ruslan Ermilov 6de4623bfe MFi386: revision 1.19. 2003-06-30 12:53:39 +00:00
Peter Wemm 1b376078fd Update for AMD64. repocopied from i386-elf/crt1.c. Deal with regparm
argument passing rather than stack based args.  The kernel passes the
base of the argument/env vector in %rdi (arg1).
2003-04-30 19:27:07 +00:00
Mark Murray 818c8b696b make these more useful for lint(1). Minor diff-reductions while I'm
about it.
2003-01-26 23:14:47 +00:00
Bruce Evans 4ce32d5dee Backed out previous commit (alignment suitable for RELENG_4) as planned
since it has been MFC'ed.  See the log message for the previous commit
for more details.  The alignment bug in gcc-3 has not been fixed, but
it is not very serious and the previous commit just moved it (as intended).

Approved by:	re (murray)
2002-12-01 17:36:18 +00:00
Bruce Evans fb0be37ded Align the stack suitably for the version of gcc in FreeBSD-4 (provided
-fomit-frame-pointer is not used).  This is mostly moot for -current
because gcc-3 does the alignment (slightly incorrectly) in main().

This patch is intended for easy MFC'ing and should be backed out in
-current soon since it causes compiler warnings and better fixes are
possible in -current.  The best fix is to do nothing here and wait for
gcc to do stack alignment right.  gcc-3 aligns the stack in main(), but
does it too late for main()'s local variables and too late for anything
called before main().  A misaligned stack is now more than an efficiency
problem, since some SSE instructions in some or all (hardware)
implementations trap on misaligned operands even if alignment checking
is not enabled.

PR:		41528:
Submitted by:	NIIMI Satoshi <sa2c@sa2c.net> (original version)
MFC after:	3 days
2002-09-29 13:42:27 +00:00
Ruslan Ermilov d2893b161b Drop support for COPY, -c has been the default mode of install(1)
for a long time now.

Approved by:	bde
2002-07-29 09:40:17 +00:00
Mark Murray 828191256b The main reason for this is to reduce diffs between all the crt1.c's.
Assembler macros are tidied up and made as similar as sanely possible.
The macros are translated into C (__inline static) functions for lint.

Declaration orders are made the same.
Declarations are all ISOfied and tidied up.

Comment contents have gratuitous diffs removed.

The net result is a bunch of crt1.c's that are 90% the same.
It may be possible to now encapsulate the differences in one
MD header, and have only one MI crt1.c file (although the macros
to do this may be ugly).

Helpful comments by:	obrien, bde
Alpha tested by:	des
i386-elf tested by:	markm
2002-07-16 12:28:50 +00:00
Mark Murray ccece3d626 Whitespace diffs only; this brings this file into the same whitespace
convention as src/lib/csu/*/crt1.c.

This will make the follow up diffs easier to see and extract.
2002-07-03 14:42:39 +00:00