From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30005 invoked by alias); 25 Apr 2006 17:09:52 -0000 Received: (qmail 29993 invoked by uid 22791); 25 Apr 2006 17:09:51 -0000 X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (66.187.233.31) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 25 Apr 2006 17:09:49 +0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.11.20060308/8.12.11) with ESMTP id k3PH9kIs024659; Tue, 25 Apr 2006 13:09:46 -0400 Received: from pobox.surrey.redhat.com (pobox.surrey.redhat.com [172.16.10.17]) by int-mx1.corp.redhat.com (8.12.11.20060308/8.11.6) with ESMTP id k3PH9gXp026346; Tue, 25 Apr 2006 13:09:43 -0400 Received: from [10.32.68.9] (vpn-68-9.surrey.redhat.com [10.32.68.9]) by pobox.surrey.redhat.com (8.12.11.20060308/8.12.11) with ESMTP id k3PH9fHF016710; Tue, 25 Apr 2006 18:09:41 +0100 Message-ID: <444E57D4.1060504@redhat.com> Date: Tue, 25 Apr 2006 17:25:00 -0000 From: Nick Clifton User-Agent: Thunderbird 1.5 (X11/20051201) MIME-Version: 1.0 To: Ames Andreas CC: binutils@sourceware.org Subject: Re: [bfd] Redirecting function calls in object files References: <552B6B925278EF478EA8887D7F9E5AC36CE7BD@tndefr-ws00024> In-Reply-To: <552B6B925278EF478EA8887D7F9E5AC36CE7BD@tndefr-ws00024> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org X-SW-Source: 2006-04/txt/msg00343.txt.bz2 Hi Ames, > I'm writing a test facility for a bunch of C++ modules. To this end I > want to 'fix' function calls within an object file to point to some > stub functions, i.e. I want to test a given class (given as an object > file) by replacing calls to instances of other classes with calls to > stubs provided by my test facility. I'm currently only interested in > pei- and elf-targets. Requiring that the object contains symbols is > no problem, because I have the sources for the classes under test. If you have the sources of the classes available, why can't you redirect the calls at the source level ? ie why not create a set of macros that alias the desired member functions. Something like: #define foo testharness_foo to replace calls to member function "foo" with calls to "testharness_foo" which you would provide in your test facility. Alternatively have you considered using the "--wrap SYMBOL" switch that is provided by the linker ? > 1) If they aren't inlined, can libbfd help me to replace the call(s) > to the original function defined in the object file under test to a > stub function defined in another object file, provided by my test > facility? If so, how? This would be difficult. If the called function is in the same compilation unit as the caller, then the compiler may have inlined it automatically, even if it does not have the "inline" qualifier. It this case you are hosed. Even if the function is not inlined, the call to it can be computed statically by the assembler, so there may not be a reloc to tell the linker where the call originates or what is being called. Now it is true that you could analyze all of the instructions in the input object files, locate all call-subroutine instructions, check their destinations, and if any of them point to the start of one of the functions you want to intercept then replace the destination address. This would be time consuming and complex but it certainly can be done. (This approach would probably be best done as a separate tool that runs post-link. It would not have to use libbfd unless you really want to). > 2) Can I use libbfd to determine if the interesting functions are > inlined within the object under test (e.g. to issue an error > message and require a recompilation with no inlining)? No. :-( Even if a function is inlined, its code (and name) may still be present in the object file. This is usually selectable by a compiler switch. Note - there is a GCC patch available which records the switches used to build an object file as an extra section inside the object file. Thus you could in theory scan this section and look for -finline or -O3 or some other dangerous switch and issue an error message that way. The patch has not been accepted into the GCC sources yet though, but you can find it documented here: http://gcc.gnu.org/wiki/Record%20GCC%20command%20line%20switches%20in%20object%20files > Is it > certain that all calls to the same function within a single > compilation unit are either inlined or not (this is more of a > compiler question, I guess)? It is a compiler question and the answer is "no". Calls via function pointers for example cannot be inlined. There are probably other cases as well, although I cannot think of any off the top of my head. Cheers Nick