Project

General

Profile

Bug #13341

crt1.o _start_crt_compiler hook not what gcc expects

Added by Andy Fiddaman 5 months ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
lib - userland libraries
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:

Description

As can be seen in https://github.com/gcc-mirror/gcc/blob/master/libgcc/config/sol2/crtpg.c#L34 - gcc uses a hook in crt1.o in order to initialise profiling.
However, the hook in the illumos crt1.o is called _start_crt_compiler instead of __start_crt_compiler (one fewer underscore).

Building a program with profiling does not produce the expected gmon.out file:

r151036% gcc -pg -o c c.c
r151036% nm c | grep _mc
[93]    |             4200656|                   0|FUNC |GLOB |0    |14     |_mcleanup
[105]   |             4201092|                   0|FUNC |GLOB |0    |14     |_mcount
[62]    |             4201184|                   0|FUNC |LOCL |0    |14     |internal_mcount

r151036% truss ./c 2>&1 | grep gmon.out
r151036%

With an updated crt1.o that has the same symbol with a double underscore:

bloody% truss ./c 2>&1 | grep gmon.out
open("gmon.out", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3

Related issues

Related to illumos gate - Bug #8609: want a position independent CRTClosedRich Lowe2017-09-04

Actions
#1

Updated by Andy Fiddaman 5 months ago

  • Related to Bug #8609: want a position independent CRT added
#2

Updated by Electric Monk 5 months ago

  • Gerrit CR set to 1082
#3

Updated by Andy Fiddaman 5 months ago

In addition to testing that gcc's profiling now works as expected, I also tested what happens if the hook returns a non-zero value.

To do this, I used this fragment with various return codes (0, 1 and 55):

int
__start_crt_compiler (int argc, char **argv)
{
        return 1;
}

Then included this in the link and checked that the behaviour was as expected for both 32- and 64-bit objects:

# No hook
% ld /usr/lib/64/crt1.o /usr/lib/64/crti.o test.o -lc /usr/lib/64/crtn.o
% ./a.out
Test

# Hook returns 0
% ld crt1-64.o hook0.o /usr/lib/64/crti.o test.o -lc /usr/lib/64/crtn.o
% ./a.out
Test

# Hook returns 1
% ld crt1-64.o hook1.o /usr/lib/64/crti.o test.o -lc /usr/lib/64/crtn.o
% ./a.out
% echo $?
1

# Hook returns 55
% ld crt1-64.o hook55.o /usr/lib/64/crti.o test.o -lc /usr/lib/64/crtn.o
% ./a.out
% echo $?
55

and for 32-bit:

% ld crt1-32.o /usr/lib/crti.o test.o -lc /usr/lib/crtn.o
% ./a.out
Test

% ld crt1-32.o hook0.o /usr/lib/crti.o test.o -lc /usr/lib/crtn.o
% file a.out
a.out:          ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped, no debugging information available
% ./a.out
Test

% ld crt1-32.o hook1.o /usr/lib/crti.o test.o -lc /usr/lib/crtn.o
% ./a.out
% echo $?
1
% ld crt1-32.o hook55.o /usr/lib/crti.o test.o -lc /usr/lib/crtn.o
% ./a.out
% echo $?
55
#4

Updated by Electric Monk 4 months ago

  • Status changed from In Progress to Closed
  • % Done changed from 0 to 100

git commit 4004e4c5da2fe3ad63f4ffae758da0dadafdf00d

commit  4004e4c5da2fe3ad63f4ffae758da0dadafdf00d
Author: Andy Fiddaman <omnios@citrus-it.co.uk>
Date:   2020-12-04T17:13:55.000Z

    13341 crt1.o _start_crt_compiler hook not what gcc expects
    Reviewed by: Robert Mustacchi <rm@fingolfin.org>
    Reviewed by: Rich Lowe <richlowe@richlowe.net>
    Approved by: Gordon Ross <gordon.w.ross@gmail.com>

Also available in: Atom PDF