public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Christopher Harvey" <cwh@eml.cc>
To: gcc-help@gcc.gnu.org
Subject: confused by -mfentry and -mrecord-mcount linker errors
Date: Mon, 10 Jan 2022 00:03:25 -0500	[thread overview]
Message-ID: <2a7ee82b-a593-4dc2-9c73-e48d4da846b2@www.fastmail.com> (raw)

Hi,

I'll try to keep this short. See the end of this email for the question. The beginning is a minimal example and the linker errros:

bash prompt>> cat a.cpp 
#include "a.hpp"
#include <ostream>

std::ostream& operator<<(std::ostream &os, const Info &info) {
    using namespace std;
    os<<hex<<info.a<<dec;
    return os;
}

bash prompt>> cat a.hpp
#pragma once

#include <ostream>

struct Info {
    int a;
};

std::ostream& operator<<(std::ostream &os, const Info &info);

bash prompt>> cat main.cpp 
#include <iostream>
#include "a.hpp"

using namespace std;

int main() {
    cout<<hex<<endl;
}

bash prompt>> g++ -pg -mrecord-mcount -mno-nop-mcount -mfentry -minstrument-return=nop5 -mrecord-return a.cpp -c -o a.o

bash prompt>> g++ -pg -mrecord-mcount -mno-nop-mcount -mfentry -rdynamic -minstrument-return=nop5 -mrecord-return main.cpp a.o
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccSjK9Ef.o: warning: relocation in read-only section `__return_loc'
`.text._ZStanSt13_Ios_FmtflagsS_' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZStanSt13_Ios_FmtflagsS_[_ZStanSt13_Ios_FmtflagsS_]' of a.o
`.text._ZStorSt13_Ios_FmtflagsS_' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZStorSt13_Ios_FmtflagsS_[_ZStorSt13_Ios_FmtflagsS_]' of a.o
`.text._ZStcoSt13_Ios_Fmtflags' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZStcoSt13_Ios_Fmtflags[_ZStcoSt13_Ios_Fmtflags]' of a.o
`.text._ZStoRRSt13_Ios_FmtflagsS_' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZStoRRSt13_Ios_FmtflagsS_[_ZStoRRSt13_Ios_FmtflagsS_]' of a.o
`.text._ZStaNRSt13_Ios_FmtflagsS_' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZStaNRSt13_Ios_FmtflagsS_[_ZStaNRSt13_Ios_FmtflagsS_]' of a.o
`.text._ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_[_ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_]' of a.o
`.text._ZSt3hexRSt8ios_base' referenced in section `__mcount_loc' of a.o: defined in discarded section `.text._ZSt3hexRSt8ios_base[_ZSt3hexRSt8ios_base]' of a.o
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

Here is the kicker: Passing -O3 to the linker creates an executable (with some warnings that I also don't really understand)

bash prompt>> g++ -O3 -pg -mrecord-mcount -mno-nop-mcount -mfentry -rdynamic -minstrument-return=nop5 -mrecord-return main.cpp a.o
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccK8XY8Y.o: warning: relocation in read-only section `__return_loc'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIE


What am I doing wrong and what do the linker warnings and errors mean? My end goal is just to add patchable bytes at each function and ret instruction. When the executable compiles it looks right. The __mcount_loc and corresponding return location sections would also be really nice to have.

Thanks!

                 reply	other threads:[~2022-01-10  5:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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-all 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=2a7ee82b-a593-4dc2-9c73-e48d4da846b2@www.fastmail.com \
    --to=cwh@eml.cc \
    --cc=gcc-help@gcc.gnu.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).