This is the mail archive of the mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more infromation.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Fortran Common Linking please comment


I have seen some discussions on this list about this problem.
PLease forgive me if it is out of context.

This is all running under Linux RH 5.2

I have reserved 2M of memory at boot time using lilo.
I can access this happily with a normal C program.

I now want to force a fortran common to occupy that space.
For now I can leave the fortran details out though.
I have defined a new section for the common with a little assembly

[root@vmedev2 f2c]# more cm.s
        .file   "cm.s"
        .version        "01.01"
.globl mycommon_
.section .xdata
        .align 4
        .type    mycommon_,@object
        .size    mycommon_,100
        .long 133169152
        .ident  "GCC: (GNU)"
==================end cm.s======================================

cc -c cm.s

Here is my main c program

#include <stdio.h>
#include <stddef.h>
#include "sm.h"

extern int * mycommon_;

int main()
        int i;
        char * ip;


        ip = alloc_shmem(127 * 0x100000,100);

        printf(" ip address == %p\n",ip);

        *ip = 21;

        printf(" before *ip == %d\n",*ip);

        sub_(&si1,&si2);  /* call fortran routine to initialize array */
        printf(" after *ip == %d\n",*ip);

                printf("%d ",mycommon_[i]);

        return 0;
===================end c3.c=========================================
cc -c c3.c to compile

The shared memory allocation program

#ifndef __SM_H
#define __SM_H

int *alloc_shmem(int sm_base,int sm_size);

void free_shmem(int *ptr,int sm_size);
=================end sm.h==========================

sm.c looks like this

  from orig by Rob Butera


#include <unistd.h>    /* close        */
#include <stdio.h>
#include <fcntl.h>     /* open         */
#include <sys/mman.h>  /* mmap, munmap */
#include "sm.h"

int *alloc_shmem(int sm_base,int sm_size)
  int  *ptr ;
  int memd ;

  if ( (memd=open("/dev/mem", O_RDWR)) < 0 ) {
    printf(" open failed \n");
    return NULL ;
  ptr = (int *) mmap((void *)sm_base, sm_size, PROT_READ | PROT_WRITE,
                          MAP_FILE | MAP_SHARED, memd, sm_base);

  if (MAP_FAILED == ptr) {
    printf(" map failed \n");
    return NULL ;
  close(memd) ;
  return ptr ;

void free_shmem(int *ptr,int sm_size)
  munmap((void *)ptr, sm_size) ;
again cc -c sm.c

the fortran program look like

        subroutine sub(i1,i2)
        integer*2 i1
        integer*2 i2
        integer int_array(10)
        common /mycommon/int_array
        do i = 1,10
           int_array(i) = i
        end do
g77 -c f.f

Having got sm.o c3.o f.o and cm.o I link them all together and try to
define the mycommon_ symbol

ld  -r -o foo.o  --defsym mycommon_=0x7f00000 c3.o sm.o cm.o f.o

then I do
ld -o foo foo.o
to produce an executable.

 address == 0x7f00000
 before *ip == 21
 after *ip == 1

This seems to work.

Any comments on how stable this is and I could do this any better.

  Phil Wilshire

Want more information?  See the CrossGCC FAQ,
Want to unsubscribe? Send a note to

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]