This is the mail archive of the crossgcc@sourceware.cygnus.com 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


Hi,

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
program

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

cc -c cm.s



Here is my main c program

=================c3.c==============================================
#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);



/*
        for(i=0;i<10;i++)
                printf("%d ",mycommon_[i]);
        printf("\n");
*/

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

The shared memory allocation program

===================sm.h===========================
#ifndef __SM_H
#define __SM_H

int *alloc_shmem(int sm_base,int sm_size);

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

sm.c looks like this
==========================sm.c========================================
/*
  sm.c

  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) ;
}
=================end
sm.c======================================================
again cc -c sm.c

the fortran program look like

==================f.f============================================================
        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
        end
============end
f.f===============================================================
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.


./foo
 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.


Regards
  Phil Wilshire

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com


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