why gfortran under cygwin can't compile correctly? - cygwin

I installed cygwin under windows and gfortran packages.
Then I write a simple f90 file
program test
implicit none
write(*,*) "hello world"
end program test
the file was named test.f90
then I open cygwin terminal and write
gfortran -o test.exe test.f90
everything seems ok, there are no errors, and a test.exe is generated.
but
./test.exe
didn't output anything. What is wrong?
The test.exe is shared here

Related

How can I run an executable file without the "./" using a MakeFile?

I want to run my program with an executable without the "./"
For example lets say I have the makefile:
all: RUN
RUN: main.o
gcc -0 RUN main.o
main.o: main.c
gcc -c main.c
So in order to run the program normally I would say in the terminal "make" then put "./RUN" to invoke the program.
But I would just like to say in the terminal "make" then "RUN" to invoke the program.
So to conclude I would just like to say >RUN instead of >./RUN inside the terminal. Is there any command I can use to do this inside the Makefile?
When I just put "RUN" in the terminal it just says command not found.
It is a matter of $PATH, which is imported by make from your environment.
You might set it in your Makefile, perhaps with
export PATH=$(PATH):.
or
export PATH:=$(shell echo $$PATH:.)
but I don't recommend doing that (it could be a security hole).
I recommend on the contrary using explicitly ./RUN in your Makefile, which is much more readable and less error-prone (what would happen if you got a RUN program somewhere else in your PATH ?).
BTW, you'll better read more about make, run once make -p to understand the builtin rules known to make, and have
CC= gcc
CFLAGS+= -Wall -g
(because you really want all warnings & debug info)
and simply
main.o: main.c
(without recipes in that rule) in your Makefile
change your makefile to
all: RUN
RUN: main.o
gcc -o RUN main.o && ./RUN
main.o: main.c
gcc -c main.c
just put ./filename in your makefile

List command is not loading source code of assembly program in gdb

I am using gdb [7.11.1] kali linux 32-bit
when I use list command to lload the source of my assembly program in gdb it displays the following error message:
'No symbol table is loaded. Use the "file" command'
I have tried the command as:
list
list line_number
In both the cases the error is same.
Please help me
Thanks! in advance
I use list command to lload the source of my assembly
List command does nothing of the sort. Rather, it lists sources that GDB has already loaded.
as -o progname.o progname.s
In your case, GDB does not load any sources because you compiled your program without any debug info. You likely want:
as -g -o progname.o progname.s
From man as:
-g
--gen-debug
Generate debugging information for each assembler source line
using whichever debug format is preferred by the target. This
currently means either STABS, ECOFF or DWARF2.
I was incurring same problem while I was trying to debug my c code for buffer overflow stuffs.
That error rises because of clean compiling without generating any debug info.
For c program, rather than normal compiling as gcc program.c try to run gcc -g -fno-stack-protector -z exec stack -o buffer program.c.
-g tells GCC to add extra information for GDB
-fno-stack-protector flag to turn off stack protection mechanism
-z execstack, it makes stack executable
This command will create a buffer binary file of your c program and hence it will fulfil all the criteria for running the list command in gdb.
Start the gdb with gdb ./buffer then type list command. It will work!!

How to make a Makefile for a program for assembly Language?

I've come across this task to build a Makefile for a program in assembly language I made (nothing fancy, like a hello world). The program is in Linux 32 bits and I'm using NASM assembler. So far I can only find Makefiles for programs for C, I'm aware that there's not much difference from one to another but I'm not familiar with this thing. What I have is this:
Program: main.o
gcc -o Program main.o
main.o: main.asm
nasm -f elf -g -F stabs main.asm
I can't tell whether this is correct or, if it does, how it works. I can't try the code because this computer doesn't have Linux. I really would like to know what's going on in the code.
First of all, read an introduction to Makefile.
Your Makefile is read by the make program on invocation. It executes either the given rule (e.g. make clean) or executes the default one (usually all).
Each rule has a name, optional dependencies, and shell code to execute.
If you use objects file you usally start your Makefile like that:
all: main.o
gcc -o PROGRAM_NAME main.o
The dependency here is main.o, so it must be resolved before the execution of the code. Make searches first for a file named main.o and use it. If it doesn't exist, it search for a rule permitting to make the file.
You have the following:
main.o: main.asm
nasm -f elf -g -F stabs main.asm
Here the rule to make main.o depends on main.asm, which is your source file so I think it already exists. The code under the rule is then executed and has to make the file matching the rule name.
If your nasm invocation is correct, then the file is created and the code of the all (or Program as you named it) is executed.
So your Makefile should work, you just have to test it to be sure :) It is barely the same as invoking:
nasm -f elf -g -F stabs main.asm && gcc -o Program main.o
As a side note, you can still test this on windows as long as you have all the tools installed. The cygwin project provides an environment where you can install make, gcc and nasm.

GDB attaching to a process where executable is deleted

I have running process but it's executable file has got deleted.
If I try to attach gdb I got following error
/home/vivek/binary/releases/20120328101511/bin/app.exe (deleted): No such file or directory.
How can I attach gdb to this process ?
Sample Test case:
Source code:
#include<stdio.h>
#include<stdlib.h>
int main(){
for (;;){
printf("Sleeping");
sleep(1);
}
}
compile it
gcc main.cc -o a.out
gcc main.cc -o b.out
Run
./a.out
Now from different terminal delete a.out.
And fire gdb attach pgrep a.out file b.out
It doesn't work.
GDB shows following error:
/tmp/temp/a.out (deleted): No such file or directory.
A program is being debugged already. Kill it? (y or n) n
Program not killed.
Try using /proc/<pid>/exe as the executable. It appears as a symbolic link these days, however, in the past it was possible to extract the deleted executable from it.
See Detecting deleted executables.
We can use following command to attach gdb
gdb <path-to-binary> <pid>
You can't. GDB needs the symbol data that's in the executable and is not being loaded by the OS when running the program.

How to run binary file in Linux

I have a file called commanKT and want to run it in a Linux terminal. Can someone help by giving the command to run this file? I tried ./commonRT but I'm getting the error:
"bash: ./commonrt: cannot execute binary file"
[blackberry#BuildMc MainApp]$ ls -al commonKT
-rwxrwxr-x. 1 sijith sijith 10314053 Feb 27 16:49 commonKT
To execute a binary, use: ./binary_name.
If you get an error:
bash: ./binary_name: cannot execute binary file
it'll be because it was compiled using a tool chain that was for a different target to that which you're attempting to run the binary on.
For example, if you compile 'binary_name.c' with arm-none-linux-gnueabi-gcc and try run the generated binary on an x86 machine, you will get the aforementioned error.
To execute a binary or .run file in Linux from the shell, use the dot forward slash friend
./binary_file_name
and if it fails say because of permissions, you could try this before executing it
chmod +x binary_file_name
# then execute it
./binary_file_name
Hope it helps
The volume it's on is mounted noexec.
:-) If not typo, why are you using ./commonRT instead of ./commonKT ??
It is possible that you compiled your binary with incompatible architecture settings on your build host vs. your execution host.
Can you please have a look at the enabled target settings via
g++ {all-your-build-flags-here} -Q -v --help=target
on your build host? In particular, the COLLECT_GCC_OPTIONS variable may give you valuable debug info. Then have a look at the CPU capabilities on your execution host via
cat /proc/cpuinfo | grep -m1 flags
Look out for mismatches such as -msse4.2 [enabled] on your build host but a missing sse4_2 flag in the CPU capabilities.
If that doesn't help, please provide the output of ldd commonKT on both build and execution host.
This is an answer to #craq :
I just compiled the file from C source and set it to be executable with chmod. There were no warning or error messages from gcc.
I'm a bit surprised that you had to 'set it to executable' -- my gcc always sets the executable flag itself. This suggests to me that gcc didn't expect this to be the final executable file, or that it didn't expect it to be executable on this system.
Now I've tried to just create the object file, like so:
$ gcc -c -o hello hello.c
$ chmod +x hello
(hello.c is a typical "Hello World" program.) But my error message is a bit different:
$ ./hello
bash: ./hello: cannot execute binary file: Exec format error`
On the other hand, this way, the output of the file command is identical to yours:
$ file hello
hello: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
Whereas if I compile correctly, its output is much longer.
$ gcc -o hello hello.c
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=131bb123a67dd3089d23d5aaaa65a79c4c6a0ef7, not stripped
What I am saying is: I suspect it has something to do with the way you compile and link your code. Maybe you can shed some light on how you do that?
The only way that works for me (extracted from here):
chmod a+x name_of_file.bin
Then run it by writing
./name_of_file.bin
If you get a permission error you might have to launch your application with root privileges:
sudo ./name_of_file.bin
Or, the file is of a filetype and/or architecture that you just cannot run with your hardware and/or there is also no fallback binfmt_misc entry to handle the particular format in some other way. Use file(1) to determine.
your compilation option -c makes your compiling just compilation and assembly, but no link.
If it is not a typo, as pointed out earlier, it could be wrong compiler options like compiling 64 bit under 32 bit. It must not be a toolchain.
full path for binary file. For example: /home/vitaliy2034/binary_file_name. Or
use directive "./+binary_file_name".
'./' in unix system it return full path to directory, in which you open terminal(shell).
I hope it helps.
Sorry, for my english language)
1st login with su
su <user-name>
enter password
Password: xxxxxx
Then executer command/file, it should run.

Resources