Hello all !

I'm still fighting to make tinycc on ARM work properly, to test it I'm
using fossil-scm as a test program because it's not a trivial program and
self contained, tinycc on linux 32 and 64 bits already is capable of
generate working programs form it but on ARM right now it compiles but the
program do not produce the correct results.

One technique I was willing to use is to generate a callgraph of the
program on linux (that is done) and then on ARM (here is the trouble).

For that I'm using a translated program from bash/awk (
http://publicclu2.blogspot.co.uk/2013/05/call-graph-generation.html) to lua
with some fixes (see attached), the other possibility would be to use
"valgrind --tool=callgrind" but the tinycc generated programs are rejected
from valgrind for several reasons:

1- "Inconsistency detected by ld.so: rtld.c: 1292: dl_main: Assertion
`_rtld_local._dl_rtld_map.l_libname' failed!" for this one I found a
workaround making a change on tccelf.c (it's on the mob but commented
because it has a side effect of programs generated with debug information
and stripped afterwards segfaults).

2- "disInstr(arm): unhandled instruction: 0xE3511000  cond=14(0xE)
27:20=53(0x35) 4:4=0 3:0=0(0x0)" on ARM the programs generated with tinycc
emits a combination of instructions that confuses valgrind (note the same
program compiled with gcc works fine with valgrind).

3- "Invalid read of size 4 ... Address 0x????? is just below the stack
ptr."  on ARM the programs generated with tinycc manage the stack in a way
that confuses valgrind/gdb (note the same program compiled with gcc works
fine with valgrind).

With GDB and the lua script attached it also fails because the way tinycc
generated programs manipulate the stack:
------
GNU gdb (GDB) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "armv7l-unknown-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from check-va-tcc...done.
(gdb) b main
Breakpoint 1 at 0x8260: file check-va.c, line 18.
(gdb) r
Starting program: /home/mingo/dev/tmp/check-va-tcc

Breakpoint 1, main () at check-va.c:18
18  myprintf ();
(gdb) s
myprintf () at check-va.c:13
13    return passdown();
(gdb) bt 2
#0  myprintf () at check-va.c:13
#1  0xbefff628 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) s
Cannot access memory at address 0x0
passdown () at check-va.c:8
8  return db_prepare();
(gdb) bt 2
#0  passdown () at check-va.c:8
#1  0xbefff618 in ?? ()
Cannot access memory at address 0x0
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) s
db_prepare () at check-va.c:3
3  return 2;
(gdb) bt 2
#0  db_prepare () at check-va.c:3
#1  0x0000822c in passdown () at check-va.c:8
Cannot access memory at address 0x0
(More stack frames follow...)
(gdb)
------

So my call for help from anyone with tinycc/ARM knowledge is how we can
make tinycc generated programs on ARM to play nice with valgrind/gdb ?
Mainly the stack issue (attached we have a obdump of the same sample with
gcc and tcc).

If we can achieve this investigate why tinycc generated programs like
fossil-scm to not work properly will be a bit easier.

Thanks in advance for your time, attention and any help !
Breakpoint 1 at 0x83c8: file check-va.c, line 3.
Breakpoint 2 at 0x83f8: file check-va.c, line 18.
Breakpoint 3 at 0x83e8: file check-va.c, line 13.
Breakpoint 4 at 0x83d8: file check-va.c, line 8.

Breakpoint 2, main () at check-va.c:18
18        myprintf ();
#0  main () at check-va.c:18

Breakpoint 3, myprintf () at check-va.c:13
13          return passdown();
#0  myprintf () at check-va.c:13
#1  0x000083fc in main () at check-va.c:18

main myprintf ()
Breakpoint 4, passdown () at check-va.c:8
8         return db_prepare();
#0  passdown () at check-va.c:8
#1  0x000083ec in myprintf () at check-va.c:13

myprintf passdown ()
Breakpoint 1, db_prepare () at check-va.c:3
3         return 2;
#0  db_prepare () at check-va.c:3
#1  0x000083dc in passdown () at check-va.c:8

passdown db_prepare ()
Breakpoint 1, db_prepare () at check-va.c:3
3         return 2;
#0  db_prepare () at check-va.c:3
#1  0x00008400 in main () at check-va.c:19
[Inferior 1 (process 6708) exited normally]
Function calls count
db_prepare      2
main    1
myprintf        1
passdown        1

Attachment: callgraph
Description: Binary data

-- Permission is hereby granted, free of charge, to any person
-- obtaining a copy of this software and associated documentation files
-- (the "Software"), to deal in the Software without restriction,
-- including without limitation the rights to use, copy, modify, merge,
-- publish, distribute, sublicense, and/or sell copies of the Software,
-- and to permit persons to whom the Software is furnished to do so,
-- subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

--based on callgraph from http://publicclu2.blogspot.co.uk/2013/05/call-graph-generation.html
--translated to lua by Domingo Alvarez Duarte mingodad_at_gmail_dot_com

local strFormat = string.format

local function capture(cmd)
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  return s
end

local prog_name="callgraph"

if  #arg < 1 then
    print( strFormat("Usage: %s EXECUTABLE [ARGS...]", prog_name))
    print( strFormat("\nExample: %s ~/bin/test-program foo 23", prog_name))
    os.exit( 1 )
end

-- Sanity checks.
local FILE=arg[1]

local fd, err = io.open(FILE)
if err then
    print(strFormat("%s: Unable to find executable '%s'", prog_name, FILE))
    os.exit( 1 )
end

fd:close()

local str = capture(strFormat("gdb --eval-command=quit %s 2>&1", FILE))

if str:match("(no debugging symbols found)") or str:match("(not in executable format)") then
    print(strFormat([==[
%s: Can't print call graph for '%s' because it's not a
binary executable compiled with debugging symbols.
]==], prog_name, FILE))
    os.exit( 1 )
end

-- Set up temporary files.
local TRACE_file_name = FILE .. ".gdb.trace"
local TRACE_fd=io.open(TRACE_file_name, "w")

local GETFUNCS_file_name = FILE .. ".gdb.funcs"
local GETFUNCS_fd=io.open(GETFUNCS_file_name, "w")

-- Take control of GDB and print call graph.
GETFUNCS_fd:write([[
set height 0
info functions
]])
GETFUNCS_fd:close()

str = capture(strFormat("gdb --batch --command=%s %s 2>/dev/null", GETFUNCS_file_name, FILE))

--print(str)
--os.exit(1)

local  total = 0;

TRACE_fd:write([[
set width 0
set height 0
set verbose off
]])

local functions_to_break = {}

for fn in str:gmatch("([a-zA-Z_][a-zA-Z0-9_]+)%b()") do
	TRACE_fd:write(strFormat("break %s\n", fn));
	functions_to_break[fn] = 0;
	total = total + 1;
end

for i = 1, total do
	TRACE_fd:write(strFormat("commands %d\n", i));
	--TRACE_fd:write("info args\n");
	TRACE_fd:write( "backtrace 2\ncontinue\nend\n");
end
TRACE_fd:write( "run\n");
TRACE_fd:close()

local cmd_args = ""
for i=2, #arg do
	cmd_args = cmd_args .. " " .. arg[i]
end
--print(FILE, cmd_args)

local cmd = strFormat("gdb --batch --command=%s --tty=/dev/null --args %s %s 2>/dev/null", TRACE_file_name, FILE, cmd_args)
--print(cmd)

local fd_cmd = assert(io.popen(cmd, 'r'))

local function joinLines(s)
	if not s:match(":%d+$") then
		--parameters have embedded new lines
		while(true) do
			local new_line = fd_cmd:read('*l')
			if not new_line then break end
			s = s .. "\\n" .. new_line
			if new_line:match(":%d+$") then
				break
			end
		end
	end
	return s
end

local isrecord = false;
local callee = "";
local caller = "*INITIAL*";
local params = "";
  
while(true) do
	local s = fd_cmd:read('*l')
	if not s then break end
	--print(s)
	if s:match("^Breakpoint [0-9]+,") then
		isrecord = true;
		s = joinLines(s)
		callee, params = s:match("^Breakpoint [0-9]+,%s*(%S+)%s*(%(.*%))%s+at%s+%S+:%d+$");
		local call_count = functions_to_break[callee]
		if call_count then
			functions_to_break[callee] = call_count + 1
		else
			print("Bad match", callee, s)
		end
	elseif s:match("^#1%s+") then
		if (isrecord) then
			s = joinLines(s)
			caller =s:match("^#1%s+%S+%s+%S+%s+(%S+)");
		end
	elseif #s == 0 then
		if (isrecord and (caller ~= "*INITIAL*")) then
			print(strFormat("%s %s %s", caller, callee, params));
			callee = "";
			caller = "";
			params = "";
		end
	else
		--print("No match line", s)
	end
end
fd_cmd:close()

local functions_calls = {}
for fn, cnt in pairs(functions_to_break) do
	table.insert(functions_calls, {fn, cnt})
end

--table.sort(functions_calls, function(a,b) return a[2] > b[2] end)
table.sort(functions_calls, function(a,b)
		if a[2] == b[2] then
			return a[1] < b[1]
		end
		return a[2] > b[2] 
	end)

print("Function calls count")
for k,v in ipairs(functions_calls) do
	print(v[1], v[2])
end
int db_prepare(void)
{
  return 2;
}

static int passdown (void)
{
  return db_prepare();
}

static int myprintf (void)
{
    return passdown();
}

int main ()
{
  myprintf ();
  db_prepare();
  return 0;
}

Attachment: check-va-mk
Description: Binary data

Attachment: check-va-gcc.o.dump.asm
Description: Binary data

Attachment: check-va-tcc.o.dump.asm
Description: Binary data

Breakpoint 1 at 0x820c: file check-va.c, line 3.
Breakpoint 2 at 0x8260: file check-va.c, line 18.
Breakpoint 3 at 0x8244: file check-va.c, line 13.
Breakpoint 4 at 0x8228: file check-va.c, line 8.

Breakpoint 2, main () at check-va.c:18
18        myprintf ();
#0  main () at check-va.c:18

Breakpoint 3, myprintf () at check-va.c:13
13          return passdown();
#0  myprintf () at check-va.c:13
#1  0xbefff628 in ?? ()
8         return db_prepare();
#0  passdown () at check-va.c:8
#1  0xbefff618 in ?? ()
3         return 2;
#0  db_prepare () at check-va.c:3
#1  0x0000822c in passdown () at check-va.c:8

passdown myprintf ()
Breakpoint 1, db_prepare () at check-va.c:3
3         return 2;
#0  db_prepare () at check-va.c:3
#1  0x00008268 in main () at check-va.c:19
[Inferior 1 (process 6694) exited normally]
Function calls count
db_prepare      1
main    1
myprintf        1
passdown        0
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to