110d63b7Richard Lowe/*
210d63b7Richard Lowe * CDDL HEADER START
310d63b7Richard Lowe *
410d63b7Richard Lowe * The contents of this file are subject to the terms of the
510d63b7Richard Lowe * Common Development and Distribution License (the "License").
610d63b7Richard Lowe * You may not use this file except in compliance with the License.
710d63b7Richard Lowe *
810d63b7Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910d63b7Richard Lowe * or http://www.opensolaris.org/os/licensing.
1010d63b7Richard Lowe * See the License for the specific language governing permissions
1110d63b7Richard Lowe * and limitations under the License.
1210d63b7Richard Lowe *
1310d63b7Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
1410d63b7Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510d63b7Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
1610d63b7Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
1710d63b7Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
1810d63b7Richard Lowe *
1910d63b7Richard Lowe * CDDL HEADER END
2010d63b7Richard Lowe */
2110d63b7Richard Lowe/*
2210d63b7Richard Lowe * Copyright 1998 Sun Microsystems, Inc. All rights reserved.
2310d63b7Richard Lowe * Use is subject to license terms.
2410d63b7Richard Lowe */
2510d63b7Richard Lowe
2610d63b7Richard Lowe#pragma init(ld_support_init)
2710d63b7Richard Lowe
2810d63b7Richard Lowe#include <stdio.h>
2910d63b7Richard Lowe#include <unistd.h>
3010d63b7Richard Lowe#include <stdlib.h>
3110d63b7Richard Lowe#include <string.h>
3210d63b7Richard Lowe#include <libelf.h>
3310d63b7Richard Lowe#include <sys/param.h>
3410d63b7Richard Lowe#include <link.h>
3510d63b7Richard Lowe
3710d63b7Richard Lowe
3810d63b7Richard Lowe/*
3910d63b7Richard Lowe * Linked list of strings - used to keep lists of names
4010d63b7Richard Lowe * of directories or files.
4110d63b7Richard Lowe */
4210d63b7Richard Lowe
4310d63b7Richard Lowestruct Stritem {
44ae389aaAndy Fiddaman	char		*str;
45ae389aaAndy Fiddaman	void		*next;
4610d63b7Richard Lowe};
4710d63b7Richard Lowe
48ae389aaAndy Fiddamantypedef struct Stritem	Stritem;
4910d63b7Richard Lowe
50ae389aaAndy Fiddamanstatic char		*depend_file = NULL;
51ae389aaAndy Fiddamanstatic Stritem		*list = NULL;
5210d63b7Richard Lowe
53ae389aaAndy Fiddamanvoid
54ae389aaAndy Fiddamanmk_state_init(void)
5510d63b7Richard Lowe{
5610d63b7Richard Lowe	depend_file = getenv(SUNPRO_DEPENDENCIES);
5710d63b7Richard Lowe} /* mk_state_init() */
5810d63b7Richard Lowe
5910d63b7Richard Lowestatic void
60ae389aaAndy Fiddamanprepend_str(Stritem **list, const char *str)
6110d63b7Richard Lowe{
62ae389aaAndy Fiddaman	Stritem *new;
63ae389aaAndy Fiddaman	char	*newstr;
6410d63b7Richard Lowe
6510d63b7Richard Lowe	if (!(new = calloc(1, sizeof (Stritem)))) {
6610d63b7Richard Lowe		perror("libmakestate.so");
6710d63b7Richard Lowe		return;
6810d63b7Richard Lowe	} /* if */
6910d63b7Richard Lowe
7010d63b7Richard Lowe	if (!(newstr = malloc(strlen(str) + 1))) {
7110d63b7Richard Lowe		perror("libmakestate.so");
7210d63b7Richard Lowe		return;
7310d63b7Richard Lowe	} /* if */
7410d63b7Richard Lowe
7510d63b7Richard Lowe	new->str = strcpy(newstr, str);
7610d63b7Richard Lowe	new->next = *list;
7710d63b7Richard Lowe	*list = new;
7810d63b7Richard Lowe
7910d63b7Richard Lowe} /* prepend_str() */
8010d63b7Richard Lowe
8110d63b7Richard Lowevoid
82ae389aaAndy Fiddamanmk_state_collect_dep(const char *file)
8310d63b7Richard Lowe{
8410d63b7Richard Lowe	/*
8510d63b7Richard Lowe	 * SUNPRO_DEPENDENCIES wasn't set, we don't collect .make.state
8610d63b7Richard Lowe	 * information.
8710d63b7Richard Lowe	 */
8810d63b7Richard Lowe	if (!depend_file)
8910d63b7Richard Lowe		return;
9010d63b7Richard Lowe
9110d63b7Richard Lowe	prepend_str(&list, file);
9210d63b7Richard Lowe
9310d63b7Richard Lowe}  /* mk_state_collect_dep() */
9410d63b7Richard Lowe
9510d63b7Richard Lowevoid
9610d63b7Richard Lowemk_state_update_exit()
9710d63b7Richard Lowe{
98ae389aaAndy Fiddaman	Stritem		*cur;
99ae389aaAndy Fiddaman	char		  lockfile[MAXPATHLEN],	*err, *space, *target;
100ae389aaAndy Fiddaman	FILE		*ofp;
101ae389aaAndy Fiddaman	extern char	*file_lock(char *, char *, int);
10210d63b7Richard Lowe
10310d63b7Richard Lowe	if (!depend_file)
10410d63b7Richard Lowe		return;
10510d63b7Richard Lowe
10610d63b7Richard Lowe	if ((space = strchr(depend_file, ' ')) == NULL)
10710d63b7Richard Lowe		return;
10810d63b7Richard Lowe	*space = '\0';
10910d63b7Richard Lowe	target = &space[1];
11010d63b7Richard Lowe
11110d63b7Richard Lowe	(void) sprintf(lockfile, "%s.lock", depend_file);
11210d63b7Richard Lowe	if ((err = file_lock(depend_file, lockfile, 0))) {
11310d63b7Richard Lowe		(void) fprintf(stderr, "%s\n", err);
11410d63b7Richard Lowe		return;
11510d63b7Richard Lowe	} /* if */
11610d63b7Richard Lowe
11710d63b7Richard Lowe	if (!(ofp = fopen(depend_file, "a")))
11810d63b7Richard Lowe		return;
11910d63b7Richard Lowe
12010d63b7Richard Lowe	if (list)
12110d63b7Richard Lowe		(void) fprintf(ofp, "%s: ", target);
12210d63b7Richard Lowe
12310d63b7Richard Lowe	for (cur = list; cur; cur = cur->next)
12410d63b7Richard Lowe		(void) fprintf(ofp, " %s", cur->str);
12510d63b7Richard Lowe
12610d63b7Richard Lowe	(void) fputc('\n', ofp);
12710d63b7Richard Lowe
12810d63b7Richard Lowe	(void) fclose(ofp);
12910d63b7Richard Lowe	(void) unlink(lockfile);
13010d63b7Richard Lowe	*space = ' ';
13110d63b7Richard Lowe
13210d63b7Richard Lowe} /* mk_state_update_exit() */
13310d63b7Richard Lowe
13410d63b7Richard Lowestatic void
13510d63b7Richard Loweld_support_init()
13610d63b7Richard Lowe{
13710d63b7Richard Lowe	mk_state_init();
13810d63b7Richard Lowe
13910d63b7Richard Lowe} /* ld_support_init() */
14010d63b7Richard Lowe
14110d63b7Richard Lowevoid
142ae389aaAndy Fiddamanld_file(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
14310d63b7Richard Lowe{
144ae389aaAndy Fiddaman	if (!((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
14510d63b7Richard Lowe		return;
14610d63b7Richard Lowe
14710d63b7Richard Lowe	mk_state_collect_dep(file);
14810d63b7Richard Lowe
14910d63b7Richard Lowe} /* ld_file */
15010d63b7Richard Lowe
15110d63b7Richard Lowevoid
15210d63b7Richard Loweld_atexit(int exit_code)
15310d63b7Richard Lowe{
15410d63b7Richard Lowe	if (exit_code)
155ae389aaAndy Fiddaman		return;
15610d63b7Richard Lowe
15710d63b7Richard Lowe	mk_state_update_exit();
15810d63b7Richard Lowe
15910d63b7Richard Lowe} /* ld_atexit() */
16010d63b7Richard Lowe
16110d63b7Richard Lowe/*
16210d63b7Richard Lowe * Supporting 64-bit objects
16310d63b7Richard Lowe */
16410d63b7Richard Lowevoid
165ae389aaAndy Fiddamanld_file64(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
16610d63b7Richard Lowe{
167ae389aaAndy Fiddaman	if (!((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
16810d63b7Richard Lowe		return;
16910d63b7Richard Lowe
17010d63b7Richard Lowe	mk_state_collect_dep(file);
17110d63b7Richard Lowe
17210d63b7Richard Lowe} /* ld_file64 */
17310d63b7Richard Lowe
17410d63b7Richard Lowevoid
17510d63b7Richard Loweld_atexit64(int exit_code)
17610d63b7Richard Lowe{
17710d63b7Richard Lowe	if (exit_code)
178ae389aaAndy Fiddaman		return;
17910d63b7Richard Lowe
18010d63b7Richard Lowe	mk_state_update_exit();
18110d63b7Richard Lowe
18210d63b7Richard Lowe} /* ld_atexit64() */