GCC Creating new PASS - gcc

I am trying to write a new GCC pass in gcc-4.1.2. This is my first attempt and I am trying to print lines of code for a function gcc is compiling. So I added the below code. Note if I remove code in execute_gimple_manipulation method the issue does not occur :
gcc/gcc-4.1.2 1223> cat gcc/gimple-manipulation.c
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "flags.h"
#include "tree.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "timevar.h"
#include "diagnostic.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "tree-chrec.h"
static void execute_gimple_manipulation (void);
static bool gate_gimple_manipulation (void);
/*-------------------------
The main driver function.
-------------------------*/
static void execute_gimple_manipulation (void)
{
basic_block bb = ENTRY_BLOCK_PTR;
int bbcounter=0, stmtcounter;
tree stmt;
block_stmt_iterator si;
/* traverse each basic block and print all statements */
FOR_EACH_BB (bb){ /* in any order */
bbcounter++;
printf("\n-> entering bb # %d (internal #: %d)\n", bbcounter, bb->index);
stmtcounter=0;
for(si = bsi_start(bb); !bsi_end_p(si); bsi_next(&si)){
stmtcounter++;
printf(" encountering statement #%2d: ", stmtcounter);
stmt = bsi_stmt(si);
print_generic_stmt (stderr, stmt, 0);
}
}
}
/* ------------------------------------------
Return true if we should execute our pass.
------------------------------------------*/
static bool
gate_gimple_manipulation (void)
{
/* return (flag_unit_at_a_time != 0 && flag_ipa_gimple_manipulation
Don't bother doing anything if the program has errors.
&& !(errorcount || sorrycount));*/
return (/*optimize >= 1*/
/* Don't bother doing anything if the program has errors. */
/*&&*/ !(errorcount || sorrycount));
}
struct tree_opt_pass pass_gimple_manipulation =
{
"gm_pass_simple", /* name */
gate_gimple_manipulation, /* gate */
execute_gimple_manipulation, /* execute */
NULL, /* sub */
NULL, /* next */
0, /* static pass number */
0, /* tv_id */
PROP_cfg | PROP_ssa | PROP_alias, /* properties required */
0, /* properties provided */
0, /* properties destroyed */
0, /* todo_flags start */
TODO_dump_func, /* todo_flags finish */
0 /* letter */
};
My new code compilation goes fine as per make:
gcc -c -g -O2 -DIN_GCC -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -pedantic -Wno-long-long -Wno-variadic-macros -Wold-style-definition -Wmissing-format-attribute -DHAVE_CONFIG_H -I. -I. -I../.././gcc -I../.././gcc/. -I../.././gcc/../include -I../.././gcc/../libcpp/include ../.././gcc/gimple-manipulation.c -o gimple-manipulation.o
But the compilation process fails further on:
gcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/xgcc -Bgcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/ -B/usr/local/x86_64-unknown-linux-gnu/bin/ -B/usr/local/x86_64-unknown-linux-gnu/lib/ -isystem /usr/local/x86_64-unknown-linux-gnu/include -isystem /usr/local/x86_64-unknown-linux-gnu/sys-include -O2 -O2 -g -O2 -DIN_GCC -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -fPIC -g -DHAVE_GTHR_DEFAULT -DIN_LIBGCC2 -D__GCC_FLOAT_NOT_NEEDED -msse -c \
../.././gcc/config/i386/crtfastmath.c \
-o crtfastmath.o
../.././gcc/config/i386/crtfastmath.c:110: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
make[2]: *** [crtfastmath.o] Error 1
make[2]: Leaving directory `gcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc'
make[1]: *** [all-gcc] Error 2
make[1]: Leaving directory `gcc/gcc-4.1.2'
make: *** [all] Error 2
gcc/gcc-4.1.2 1223>
Any help – why the issue is occurring and how can I achieve the objective?

Related

Cannot link dlfcn functions with gcc and -ldl

I am trying to use functions in dlcfn.h, to print custom trace from my C application.
I do link with -ldl, yet the linker complains that it cannot find dladdr().
What am I missing?
Here is an example, inst.c:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
__attribute__((no_instrument_function))
void __cyg_profile_func_enter (void *this_fn, void *call_site) {
printf("%p\n", this_fn);
Dl_info info;
if (dladdr(this_fn, &info)) {
printf("%p [%s] %s\n",
this_fn,
info.dli_fname ? info.dli_fname : "?",
info.dli_sname ? info.dli_sname : "?");
}
}
__attribute__((no_instrument_function))
void __cyg_profile_func_exit (void *this_fn, void *call_site) {
}
void my_function_b(void)
{
printf("b!\n");
}
void my_function_a(void)
{
printf("a!\n");
my_function_b();
}
int main(void)
{
my_function_a();
my_function_b();
return 0;
}
And a Makefile:
CFLAGS += -O0 -g -finstrument-functions -rdynamic -ldl
inst: inst.c
gcc $(CFLAGS) $^ -o $#
The result:
$ make
gcc -O0 -g -finstrument-functions -ldl inst.c -o inst
/usr/bin/ld: /tmp/ccKL1sh2.o: in function `__cyg_profile_func_enter':
/home/gauthier/tmp/inst/inst.c:12: undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
make: *** [Makefile:4: inst] Error 1
You should put libraries after source or object files which import their functions, otherwise linker will optimize them out:
gcc -O0 -g -finstrument-functions inst.c -ldl -o inst

Undefined reference to sequence when trying to input a vector

This seems a simple but somehow the compile sends this error message which I'm not able understand thus correct my code.
This is a simplified version of what I did, just so it can appear the error for you:
Main.cpp
include "myfunction.h"
int main(){
std::vector<int> myVet = {1,4,3};
sequence(1,2,1,myVet);
}
myfunction.h
#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end,
int step, std::vector<int> skip);
myfunction.cpp
#include "myfunction.h"
void sequence(int start, int end,
int step, std::vector<int> skip){
auto x = 0;
};
This gives me an error message which says
In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
Could you please explain me why it appears?
This is the following command which I've been using for compiling
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp -o main.out
You are only passing main.cpp to g++.
g++ needs to know about myfunction.cpp where your function is defined so as to compile and link it to your program.
The command to use should be:
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out

what is the gcc option for add header file in complie time?

How to compile this code?
/* main.c file*/
#include <stdio.h>
int main(void)
{
int c;
c = add(5,5);
printf("sum is %d\n",c);
return 0;
}
Add.c(in same directory)
/*add.c*/
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
Add.h(in same directory)
/* add.h file*/
int add(int, int);
Then i am create a object file for add.c
$ gcc -c -Wall add.c -I.
Then i am try to create a object file for main.c
$ gcc -c -Wall main.c -I.
main.c:6:2: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
c = add(5, 5);
^
Please any one tell, how to give the headfile name in compiling time.
you can either use #include "Add.h" at the top of your main.c (this is the preferred route), or you can use the -include command line flag (this is discouraged because it does not scale and is not portable):
gcc -c -Wall main.c -I. -include Add.h

How to use shared library

These are my C codes simply print “Hello" Message. And I want to make mylib.c as shared library.
[mylib.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
int mylib(){
printf("### Hello I am mylib #####\n");
return 0;
}
[drive.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
At the firest I compiled mylib.c with folowing command line to make mylib.o
gcc –fPIC –g –c –Wall mylib.c
Then tried to make it shared librarly like this
gcc -shared -Wl,-soname,libmylib.so.1 -o /opt/lib/libmylib.so.1.0.1 mylib.o -lc
And I did ldconfig to update /etc/ld.so.cache
Finaly I compiled drive.c link with mylib but linker showed error
gcc -g -Wall -Wextra -pedantic -I./ -L./ -o drive drive.c –lmylib
/usr/bin/ld: cannot find –lmylib
Dose someone tell me how can I compile it?
In my way, you have to follow some ways to use shared library in C.
At first I have created a header file named "shared_library.h", in this file I have introduced a function named "method" as a function of this library.
The code is following:
/*-------This is starting of shared_library.h file-----------*/
void method();
/*-------------This is ending of shared_library.h file--------*/
Then I have defined the method in another file named "shared_library.c". The definition as in code is:
/*-------------This is starting of shared_library.c file---------*/
#include "shared_library.h"
void method()
{
printf("Method is called");
}
/*-------------This is ending of shared_library.c file---------*/
And finally, the header "shared_library.h" is ready to use. I use the library in my main C file named "main.c". The contents of "main.c" are as follows:
/*-------------This is starting of main.c file----------------*/
#include <stdio.h>
#include "shared_library.h"
int main()
{
method();
return 0;
}
/*-------------This is ending of main.c file----------------\*/
I found this article ld cannot find an existing library.
It works if I change to gcc -g -Wall -Wextra -pedantic -I./ -L/opt/lib -o drive drive.c –l:libmylib.so.1

How to install PCAP for ruby on Windows

This must be a relatively newb question but I am still stuck. I know there is a simple solution to this. I've done some googling but could not find an exact answer. Here is the error I get...
C:\Ruby193\include\ruby-1.9.1\i386-mingw32>gem install pcap
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
ERROR: Error installing pcap:
ERROR: Failed to build gem native extension.
C:/Ruby193/bin/ruby.exe extconf.rb
checking for socket() in -lsocket... no
checking for gethostbyname() in -lxnet... no
checking for hstrerror()... no
checking for pcap.h... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Clearly I am missing some libraries but I not know how to get these nor do I know where they should be located when attempting to install pcap.
mkmf.log
have_library: checking for socket() in -lsocket... -------------------- no
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lshell32 -lws2_32 -limagehlp "
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5: int main() {return 0;}
/* end */
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lsocket -lshell32 -lws2_32 -limagehlp "
c:/users/user/downloads/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lsocket
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { void ((*volatile p)()); p = (void ((*)()))socket; return 0; }
/* end */
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lsocket -lshell32 -lws2_32 -limagehlp "
conftest.c: In function 't':
conftest.c:8:1: error: too few arguments to function 'socket'
c:\users\user\downloads\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/winsock2.h:553:35: note: declared here
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { socket(); return 0; }
/* end */
--------------------
have_library: checking for gethostbyname() in -lxnet... -------------------- no
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lxnet -lshell32 -lws2_32 -limagehlp "
c:/users/user/downloads/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lxnet
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { void ((*volatile p)()); p = (void ((*)()))gethostbyname; return 0; }
/* end */
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lxnet -lshell32 -lws2_32 -limagehlp "
conftest.c: In function 't':
conftest.c:8:1: error: too few arguments to function 'gethostbyname'
c:\users\user\downloads\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/winsock2.h:555:57: note: declared here
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { gethostbyname(); return 0; }
/* end */
--------------------
have_func: checking for hstrerror()... -------------------- no
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lshell32 -lws2_32 -limagehlp "
conftest.c: In function 't':
conftest.c:8:53: error: 'hstrerror' undeclared (first use in this function)
conftest.c:8:53: note: each undeclared identifier is reported only once for each function it appears in
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { void ((*volatile p)()); p = (void ((*)()))hstrerror; return 0; }
/* end */
"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -L. -LC:/Ruby193/lib -L/usr/local/lib -lmsvcrt-ruby191 -lshell32 -lws2_32 -limagehlp "
C:\Users\USER\AppData\Local\Temp\ccbjKHSc.o:conftest.c:(.text+0x19): undefined reference to `hstrerror'
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5:
6: /*top*/
7: int main() {return 0;}
8: int t() { hstrerror(); return 0; }
/* end */
--------------------
have_header: checking for pcap.h... -------------------- no
"gcc -E -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -I/usr/local/include conftest.c -o conftest.i"
conftest.c:5:18: fatal error: pcap.h: No such file or directory
compilation terminated.
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <winsock2.h>
4: #include <windows.h>
5: #include <pcap.h>
/* end */
--------------------
The end solution was to not use pcap rub which seems to be fairly outdated.
I tried PCAP rub and continued to run into issues. You can follow the thread here where Jonm ultimately helped me to get up and running.

Resources